您的位置:首页 > Web前端 > JavaScript

js note 2

2016-06-11 14:06 232 查看

js 构造get请求

同步

function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false 同步
xmlHttp.send( null );
return xmlHttp.responseText;
}


异步

function httpGetAsync(theUrl, callback)//callback 回调方法
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true 异步
xmlHttp.send(null);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xmlhttprequest get