Asynchronous JavaScript and XML(Ajax)

原生JavaScript的写法:

来源

var xmlReq;
if (window.XMLHttpRequest)  {// code for all new browsers
  xmlReq=new XMLHttpRequest();

}else if (window.ActiveXObject) {// code for IE5 and IE6
  xmlReq=new ActiveXObject("Microsoft.XMLHTTP");

}

if (xmlReq!=null){
    xmlReq.open("POST", url  );
    xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlReq.setRequestHeader("Content-length", paramsSend.length);
    xmlReq.setRequestHeader("Connection", "close");
    xmlReq.onreadystatechange = successResponse;        
    xmlReq.send(paramsSend);

} else {
  alert("Your browser does not support XMLHTTP.");
}

注意: 在FF里面需要将open方法放在setRequestHeader之前,否则会出错。
xmlReq.open(“POST”, “demo_dom_http.asp”, false);
xmlReq.send(xmlDoc);
document.write(xmlReq.responseText);

jQuery写法:

$.ajax({
   type: "GET",
    url: "default.aspx",
    beforeSend: function(request) {
        request.setRequestHeader("Client-Agent", "xxxx");
        request.setRequestHeader("Cookie", "xxxx");
        request.setRequestHeader("Action", "getxxxxe");
        //request.setRequestHeader("user-id", "");
        //request.setRequestHeader("Content-Type", "application/xml");
        request.setRequestHeader("APIVersion", "1.0.0");
        request.setRequestHeader("x-up-calling-line-id", "xxxx");
        request.setRequestHeader("Test", "Chenxizhang");
    },
    success: function(result) {
        alert(result);
    }
});

你可能感兴趣的:(Javascript)