我如何用JavaScript';什么是内置函数?
可能重复:我如何用JavaScript';什么是内置函数?,javascript,jquery,ajax,http-post,Javascript,Jquery,Ajax,Http Post,可能重复: 如何在不使用JQuery或任何其他库的情况下调用下面的JQuery Ajax,而只使用JavaScript内置函数 var input = '{ "age":100, "name":"foo", "messages":["msg 1","msg 2","msg 3"], "favoriteColor" : "blue", "petName" : "Godzilla", "IQ" : "QuiteLow" }'; var endpoin
如何在不使用JQuery或任何其他库的情况下调用下面的JQuery Ajax,而只使用JavaScript内置函数
var input = '{
"age":100,
"name":"foo",
"messages":["msg 1","msg 2","msg 3"],
"favoriteColor" : "blue",
"petName" : "Godzilla",
"IQ" : "QuiteLow"
}';
var endpointAddress = "http://your.server.com/app/service.svc";
var url = endpointAddress + "/FindPerson";
$.ajax({
type: 'POST',
url: url,
contentType: 'application/json',
data: input,
success: function(result) {
alert(JSON.stringify(result));
}
});
试试这个
首先,您必须创建window.XMLHttpRequest的对象
或ActiveXObject(对于IE)
然后您可以发送请求
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
最后您可以得到响应
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
看看Google的第一个答案。下面的代码完成了jQuery版本的所有功能:
- 以JSON作为postdata的POST请求
- 设置JSON内容类型标头
- 警告严格化的响应
部分取自。jQuery很好地规范了浏览器之间ajax调用的所有小怪癖 我建议找到一个独立的ajax库,它可以做同样的事情,但不需要jQuery带来的额外开销。以下是一些:
- (受jquery/zepto启发)