代碼如下 |
複製代碼 |
var url = "b.aspx"; var param = "a=1&b=2"; var ret = GetRemoteText(url, param, 'post'); // 這個ret就是b.aspx返回的內容,使用吧 // 建立XmlHttp對象 function CreateXmlHttp() { if (window.XMLHttpRequest) return new XMLHttpRequest(); else { var ver = ["MSXML2.XmlHttp.6.0", "MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"]; for (var i = 0; i < ver.length; i++) try { var XmlHttp = new ActiveXObject(ver[i]); return XmlHttp; } catch (oError) {} } return false; } // 訪問 function GetRemoteText(url,param,method){ if(method && method.toLowerCase() == 'post') method = 'POST'; else{ method = 'GET'; } var xml = CreateXmlHttp(); xml.open(method, url, false); if(method == 'POST') xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xml.send(param); if(xml.readyState == 4 && xml.status == 200) return xml.responseText; else return null; } |
這樣我們就可以把指定網站或頁面地址的內容給採集下來了。