开发者社区> 问答> 正文

getjson不执行回调函数

为什么getJSON的回调函数不能执行,新手求指教

<?php
header('Content-Type: text/json');
$a = array(
    'code'=>'CA1998',
    'price'=>'6000',
    'tickets'=>'20',
);
$result = json_encode($a);
//echo "aa($result)";
echo $result;
exit;

<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <script type="text/javascript" charset="utf-8" src="http://202.198.133.190/andrew2/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" charset="utf-8">
        function hey(){
        alert("hello");
                $.getJSON("http://202.198.133.190/andrew2/jsonp.php?callback=?",function(data){
                document.write(data.code);
                });
                }
    </script>
</head>
<body>
    <div class="main" style="margin:0 auto"><input type="button" value="点击返回json" onclick="hey()"></div>
</body>
</html>

展开
收起
a123456678 2016-07-08 10:10:42 2931 0
1 条回答
写回答
取消 提交回答
  • 跨域调用的时候,服务端会接收到一段$_GET['callback']的字符串,你需要在服务端将这段字符串拼接在需要返回的数据的前面共同返回

    2019-07-17 19:53:08
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载