When sending an AJAX request, you can create a custom header message by XMLHttpRequest This object, in the jquery framework, for the $.ajax, $.get, or $.post method of requesting the content of the Web page. It will pass a http_x_requested_with parameter to the server, PHP is in the header layer to determine whether the AJAX request, corresponding to $_server[' Http_x_requested_with '] judgment. In general $_server[' Http_x_requested_with '] default is xmlhttprequest,$_server[' Http_x_requested_with ') can also be custom created, Use Xmlhttprequest.setrequestheader (name,value). Example: A front-end page sends a normal AJAX request to the backend test.php.
$.ajax ({type: "GET", url: ' test.php ', success:function (data) {Console.log (data); }}); The server-side test.php can determine whether the request is an AJAX asynchronous request and then respond to the business needs. The following is a simple validation of the server-side test.php is the code for the AJAX request: function Isajax () {return @$_server[' http_x_requested_with '] = = ' XMLHttpRequest '? t Rue:false; } if (Isajax ()) {echo ' Ajax Request Success. '; } else {echo ' No. '; }ajax Initiate JSONP cross-domain request we can implement the cross-domain Ajax request through the Jsonp way of jquery, the service side PHP also needs to make corresponding processing, that is to say PHP this side must and front page to request and return data in certain format. Example: Front-end page initiates JSONP request: $.ajax ({type: "Get", Data: "Random=" +math.random (), url: "Http://demo.thinkphp.net/phpaja X/jsonp.php ", DataType:" Jsonp ", Jsonp:" Callback ", Success:function (data) {Console.log (data); }, Error:function () {console.log (' Request error. '); }}); We will find that there are datatype in the AJAX request parameters: "Jsonp" and Jsonp: "Callback", which indicates that I want to request JSONP, and there will be a callback callback return. Of course, we can also customize the callback function, such as Jsonpcallback: "Success_jsonpcallback" can also be simply written: Jquery.getjson (' http://demo.thinkphp.net/phPajax/jsonp.php?callback=? ", {Random:Math.random ()}, function (data) {Console.log (data); });p HP Backend service code can be written like this (note the format returned by the output): $data = Array (' rand ' = $_get[' random '), ' msg ' = ' Success '); Echo $_get[' callback ']. ' ('. Json_encode ($data). ') '; Ajax cross-domain request: cors cors, also known as cross-domain resource sharing, English full name Cross-origin Resource sharing. Suppose we want to use Ajax to point data from a.com pages to b.com pages, usually because of the same origin policy, this request is not allowed, the browser will return "source mismatch" error, so there is a "cross-domain" this term. But we also have a solution, we can b.com the page header information to add a line of code: Header ("Access-control-allow-origin: *"), when we set the header for the above information, Any request comes after the service side we can handle and response, then in the debugging tool can see its header information settings, where there is a red box information is "*access-control-allow-origin:*", indicating that we have enabled Cors, If you want to restrict a request that only allows a domain name, you can: header ("access-control-allow-origin:http://www.thinkphp.com"); Example: cross-domain Request data $.ajax via Cors ({type : "Get", Data: "Random=" +math.random (), url: "http://demo.thinkphp.net/phpajax/ajax.php", DataType: "JSON", Success:function (data) {Console.log (data); $ ("#result_3"). html (data.msg+ ': ' +data.rand); }, Error:function () { $ ("#result_3"). html (' Request Error. '); }}); we add this code to the ajax.php of another website domain name: header ("access-control-allow-origin:http://www.thinkphp.com"); $data = Array (' rand ' = $_get[' random '], ' msg ' = ' Success '); echo Json_encode ($data);