AJAX 常用函数创建XMLHTTP对象,区别IE,Mozilla浏览器
程序员文章站
2022-04-20 13:34:34
ajax 常用函数创建xmlhttp对象,区别ie,mozilla浏览器的函数,网上有很多创建xmlhttp对象,区别ie,mozilla浏览器的函数,这个比较不...
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;
}
复制代码 代码如下:
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;
}