深圳全飞鸿
标题:
jsonp服务端代码
[打印本页]
作者:
zhgc
时间:
2020-8-19 22:36
标题:
jsonp服务端代码
$callback = $_REQUEST['callback'];
// Create the output object.
$output = array('a' => 'Apple', 'b' => 'Banana');
//start output
if ($callback) {
header('Content-Type: text/javascript');
echo $callback . '(' . json_encode($output) . ');';
} else {
header('Content-Type: application/x-json');
复制代码
作者:
zhgc
时间:
2020-8-19 23:51
jsonp本地代码
<script>
$(function(){
getData();
setInterval(function(){
var head = document.head || $('head')[0];
var script = $(head).find('script')[0];
if (script){
var src = script.src || '';
var idx = src.indexOf('callback=');
if(idx != -1){
var idx2 = src.indexOf('&');
if (idx2 == -1){idx2 = src.length;}
var jsonCallback = src.substring(idx + 9, idx2);
delete window[jsonCallback];
if (script.parentNode){
script.parentNode.removeChild(script);
}
}
}
getData();
}, 1000);
});
function getData(){
jQuery.ajax({
//src="http://127.0.0.1:808/hello1.html?callback=zz&_=1597846439408"
//src="http://127.0.0.1:808/hello1.html?callback=jQuery17206514656749972916_1597846503900&_=1597846534352
url:"http://127.0.0.1:808/hello1.html?BB=17",
type:"GET",
dataType:"jsonp",
//jsonp: "callback",
//jsonpCallback:"zz",
success:function(result){
alert(result);
} ,
error:function(xOptions, textStatus){console.log(textStatus);}
})
};
</script>
复制代码
作者:
zhgc
时间:
2023-8-21 19:54
本帖最后由 zhgc 于 2023-8-21 20:07 编辑
JSONP调用中, jsonp和jsonpCallback 属性是什么用
[backcolor=rgba(255, 255, 255, 0.86)]
在 JSONP 中,jsonp 是回掉函数名的参数名,默认为 callback,服务端通过它来获取到回掉函数名。而 jsonpCallback 则是回掉函数名,默认由 jQuery 自动生成 。
src="http://127.0.0.1:808/hello1.html?callback=jQuery17206514656749972916_1597846503900&_=1597846534352
那jsonp: "callback",
服务器通过josnp属性得到参数名是callback, 然后通过callback名得到jQuery17206514656749972916_1597846503900&_=1597846534352
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2