jquery jsonp实现跨域

html代码:

 1 $("#testJsonp").click(function(){
 2     $.ajax({
 3         url: "http://www.test.cc/1.php",                        
 4         type:'get',
 5         async:false,
 6         jsonpCallback: "receive",
 7         jsonp: "callback",
 8         // tell jQuery we're expecting JSONP
 9         dataType: "jsonp",                        
10         success: function( data ) {             
11             alert(data.status);
12             console.log( data ); // server response
13         }
14     });
15 });

1.php

1 $arr = array(
2     'status' => 1,
3 );
4 echo $_GET['callback']."(".json_encode($arr).")";   

需要注意的是:

jsonpCallback:需要设置,然后在php代码返回时,需要拼接成一个js方法。
只支持get跨域是妥妥地
原文地址:https://www.cnblogs.com/300js/p/5198898.html