1

所以正如我提出的问题:$.ajax() jQuery 缩短普通代码的方式(这些都是不同的例子,因为我的问题只是关于结构)是

  $.ajax(
        {url:"index.php/a", 
        type:"POST",
        contentType:"application/json; charset=utf-8",
                    data:{some_string:"blabla"},
        dataType:"json",
        success:function(data){
            alert(data);
            },
        error:function(a,b,c){
            }
        });

一样

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
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();
}
4

1 回答 1

1

它不完全相同,但是是的,它基本上服务于相同的目的。(尽管这两个示例的功能之间存在显着差异,其中一个是发送 POST 请求,另一个是 GET 请求)

I would suggest taking a look at the (uncompressed) source code for jQuery to see what the $.ajax() function does. It is quite a lot more complex than your raw XMLHttpRequest code, but yes it does do basically the same job, and at the core of it, it does call the same XMLHttpRequest class.

于 2013-10-10T08:45:55.027 回答