AJAX 常用函数创建XMLHTTP对象,区别IE,Mozilla浏览器
时间:2020-10-28 21:47:51|栏目:JavaScript代码|点击: 次
AJAX 常用函数创建XMLHTTP对象,区别IE,Mozilla浏览器的函数,网上有很多创建XMLHTTP对象,区别IE,Mozilla浏览器的函数,这个比较不错,大家可以直接拿用了
function getRequest() {
http_request = false;
if (window.XMLHttpRequest) {
//对于Mozilla?pNetscape?pSafari等浏览器,创建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?pNetscape?pSafari等浏览器,创建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;
}
上一篇:详解JavaScript中getFullYear()方法的使用
栏 目:JavaScript代码
本文标题:AJAX 常用函数创建XMLHTTP对象,区别IE,Mozilla浏览器
本文地址:http://www.codeinn.net/misctech/16782.html