1.ajax native
ajax includes the following steps: 1. Create an AJAX object; 2. Issue an HTTP request; 3. Receive data returned by the server; 4. Update web page data. To summarize, in one sentence, ajax issues an HTTP request through the native XMLHttpRequest
object, and then processes the data returned by the server.
Steps:
var xhr = createXHR();//创建对象 xhr.open(“方式”,”地址”,”标志位”);//初始化请求 xhr.setRequestHeader(“”,””);//设置http头信息 xhr.onreadystatechange =function(){}//指定回调函数 xhr.send();//发送请求
/ //1.创建xhr对象 var xhr; if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); }else{ xhr = new ActiveXObject('Microsoft.XMLHTTP'); } //异步接受响应 xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ //实际操作 ; } } } //发送请求 xhr.open('get',url,true); xhr.send();
2.jquery encapsulates ajax
$.ajax({ type : "get", url : '', dataType : "json", data:{}, success : function(data){ },error:function(){ console.log('fail'); } });
3.jsonp cross-domain principle
For security reasons , the browser prohibits ajaxcross-domain data acquisition
Step by step demonstration ofscriptsrc Attribute loadingjsFile method to obtain data
Explain the mechanism of cross-domain data acquisition
(1) Dynamic creation scriptTag
(2)Define callback function
(3) Return function call
(4) Pass parameters or global variables
The above is the detailed content of A brief introduction to ajax. For more information, please follow other related articles on the PHP Chinese website!