ajax 跨域 jsonp 处理
发布时间:2020-12-16 02:08:12 所属栏目:百科 来源:网络整理
导读:客户端 !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 titleUntitled Page/title script type="text/javascript" src="http:/
客户端<!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)"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |