dqqpf32897 2014-04-10 22:36
浏览 42
已采纳

Echo'ing php作为ajax调用的“返回值”

My jquery code:

    var value = $.ajax({
            type: "POST",
            url: "to_submit.php",
            data: $('#submit').serialize(),
            cache: false,
            async: true
        }).success(function(response){
            console.log(response);
            console.log("data received");
            console.log(response.data);
            $("#appended").append(response);});
        }).error(function() {});
        });

and PHP

include("../header.php");
$to_return = "test";
echo "
data: " . json_encode($to_return);
flush();

How the console.logs in the javascript part behaves:

first console.log: (a lot of code information (head, body, etc) related to my website header!)

still first console.log: data: "test"

console.log("data received");

second console.log: undefined

So, data is being recognized as undefined and a lot of code is being flushed, and I can't figure out why...

  • 写回答

2条回答 默认 最新

  • dre93205 2014-04-10 22:42
    关注

    Two things: first, you shouldn't need to use flush() - simply echo your data and end your script.

    Second, the results of your echo statement: data: ["insert your data here"] is not valid JSON and you're not going to be able to decode it on the client.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样