我如何用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启发)

我真的得问问。。。为什么?MDN对jQuery和其他库在后台为您所做的工作有很好的描述。您尝试过什么?即使谷歌也能找到答案,而不需要其他人为你复制/粘贴。如今似乎也很有希望