AJAX 常用函數建立XMLHTTP對象,區別IE,Mozilla瀏覽器的函數,網上有很多建立XMLHTTP對象,區別IE,Mozilla瀏覽器的函數,這個比較不錯,大家可以直接拿用了
複製代碼 代碼如下:function getRequest() {
http_request = false;
if (window.XMLHttpRequest) {
//對於Mozilla﹑Netscape﹑Safari等瀏覽器,建立XMLHttpRequest
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
//如果伺服器響應的header不是text/xml,可以調用其它方法修改該header
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
// 對於Internet Explorer瀏覽器,建立XMLHttpRequest
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return http_request;
}
//獲得Url 的responseText
function getResponseText(url)
{
http_request=getRequest();
http_request.open('GET', url, false);
http_request.send(null);
if (http_request.readyState == 4)
{
// 收到完整的伺服器響應
if (http_request.status == 200) {
//HTTP伺服器響應的值OK
requestdoc = http_request.responseText;
//將伺服器返回的字串寫到頁面中ID為message的地區
}
else {
requestdoc = http_request.status;
}
}
return requestdoc;
}
//獲得Url 的responseXML
程式碼片段
function getResponseText(url)
{
http_request=getRequest();
http_request.open('GET', url, false);
http_request.send(null);
if (http_request.readyState == 4)
{
// 收到完整的伺服器響應
if (http_request.status == 200) {
//HTTP伺服器響應的值OK
requestdoc = http_request.responseXML;
//將伺服器返回的字串寫到頁面中ID為message的地區
}
else {
requestdoc = http_request.status;
}
}
return requestdoc;
}