Home > Article > Web Front-end > A brief introduction to jsonp usage summary_jquery
Jsonp (JSON with Padding) is a "usage mode" of json, which allows web pages to obtain data from other domain names (websites), 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.
The same-origin policy is a well-known security policy proposed by Netscape. All browsers that support JavaScript now use this policy.
First of all: jsonp is a tool used by json to cross domains.
The principle is to bypass the same-origin policy through the cross-domain feature of the script tag.
Tested and experimented:
Sender:
$.ajax({ type : "post", url : "ajax.php", dataType : "jsonp", jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback:"jsonpcallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 success : function(json){ alert('success'); }, error:function(){ alert('fail'); } });
Server side (php):
<?php $data = "aaa"; $callback = $_GET['callback']; echo $callback.'('.json_encode($data).')'; exit; ?>