首页 > 开发 > AJAX > 正文

AJAX 常用函数创建XMLHTTP对象,区别IE,Mozilla浏览器

2024-09-01 08:31:46
字体:
来源:转载
供稿:网友


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;
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表