dtvgo28624 2015-02-18 10:44
浏览 78
已采纳

在客户端和php服务器之间发送json数据

this is the server side code

<?php

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 2016 00:00:00 GMT');

header('Content-type: application/json');
$id = $_GET['id'];
$data = array("Hello", $id);
echo json_encode($data);

?>

and this is the client side code

<html>
<head>
<script src="jquery-1.1.1.min.js"></script>
<script language="javascript">
    function show(json) {
        alert(json);
    }
    function run() {
        $.getJSON("/localhost/jserver1.php",
            { id: 567 },
        show);
    }
    window.onload=run;
</script>
</head>
<body>
    JSON Test Page.
</body>
</html>

What i want to do here is i want to send messages between client and server using php and json. when i run the server side code i am getting the output as hello with the id given in the url in the localhost,but when i run the client side code i am getting only the body of the html page,I am not getting the alert with the input id that is there in the run method.Please someone tell me what is the problem

  • 写回答

4条回答 默认 最新

  • douqie3391 2015-02-18 11:04
    关注

    Instead of

        $.getJSON("URL",  
                { id: 567 },
               show 
                );
    window.onload=run;
    

    Use -

        $.getJSON("URL",  
                { id: 567 },
               function(response){
                 show(response);
          });
    
    window.onload=run();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决