服务端PHP代码
header('Content-Type:application/json; charset=utf-8');
$arr = array('a'=>1, 'b'=>2, 'c'=>3);
$json = json_encode($arr);
$callback = $_GET['callback'];
echo $callback."($json)";
//注意callback是js那边传来的参数名称
js跨域请求的几种方式:
1,getJSON
$.getJSON('http://192.168.151.126/test?callback=?',function(data){
alert(data);
});
注意:等号后面必须是?,否则出错
2,ajax
$.ajax({
url:"http://192.168.151.126:8031/test",
dataType:'jsonp',
data:'',
jsonp:'callback',
success:function(result){
alert(result);
}
});