Jsonp (JSONwithPadding) is a type of json & quot; usage mode & quot;, which allows the webpage to retrieve data from other domain names (websites), that is, to read data across domains, this article briefly introduces the use of jsonp. For more information, see Jsonp (JSON with Padding), which is a "usage mode" of json and allows the webpage to access other domain names (websites) obtain the data, that is, read data across domains.
Why do we need a special technology (JSONP) to access data from different domains (websites? This is because of the same-origin policy.
Same-origin policy, a well-known security policy proposed by Netscape, is now used by all browsers that support JavaScript.
First, jsonp is a json cross-domain object.
The principle is to bypass the same-origin policy through the cross-origin feature of the script tag.
Tested experiment:
Sender:
$. Ajax ({type: "post", url: "ajax. php ", dataType:" jsonp ", jsonp:" callback ", // The parameter name that is passed to the request handler or page to obtain the jsonp callback function name (default: callback) jsonpCallback: "jsonpcallback", // custom jsonp callback function name. By default, the random function name automatically generated by jQuery is success: function (json) {alert ('success') ;}, error: function () {alert ('failed ');}});
Server (php ):
<? Php $ data = "aaa"; $ callback =$ _ GET ['callback']; echo $ callback. '('. json_encode ($ data ). ')'; exit;?>