您的位置:首页 > Web前端 > JavaScript

ajax 跨域 jsonp 处理

2015-01-21 11:57 309 查看

客户端

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
type : "get",
async:false,
url : "http://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=SBDBZ-D2HH4-O6FUD-XCUUY-C5SZ7-QXBWC&get_poi=0&output=jsonp",
dataType : "jsonp",
jsonp:"aa",  //一般为callback
jsonpCallback:"QQmap",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
success : function(json){
alert(json.status);
alert(json);
},
error:function(){
alert('fail');
}
});

});
</script>
</head>
<body>
</body>
</html>


jsonp jsoncallback 说明,最后会在url后加上对应的参数



响应处理



php服务端
<?php

//服务端返回JSON数据

$arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

$result=json_encode($arr);

//动态执行回调函数

$callback=$_GET['callback'];

echo $callback."($result)";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: