1 回答
TA贡献1934条经验 获得超2个赞
这里有个简单的例程:
1,创建并获取XMLHttpRequest对象:
function getXMLHttpRequest()
{
var http_request = false;
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{}
}
}
if (!http_request)
{
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
return http_request;
}
2,使用XMLHttpRequest发送请求并处理数据:
function make_request(url)
{
http_request = getXMLHttpRequest();
if (http_request)
{
http_request.onreadystatechange = function ()
{
//调用自己的方法(获取数据http_request.responseText)
//如:alertContents(http_request);
};
http_request.open('GET', url, true);
http_request.send(null);
}
}
此函数为上面的方法调用:
function alertContents(http_request)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
parse_data(http_request.responseText);
}
}
}
- 1 回答
- 0 关注
- 869 浏览
添加回答
举报