gpt4 book ai didi

javascript - 将 php 作为 "return value"回显到 ajax 调用

转载 作者:行者123 更新时间:2023-11-30 08:44:44 26 4
gpt4 key购买 nike

我的jquery代码:

    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() {});
});

PHP

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

javascript 部分中的 console.logs 的行为方式:

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

因此,数据被识别为undefined 并且大量代码被刷新,我不明白为什么...

最佳答案

两件事:首先,您不需要使用 flush() - 只需echo 您的数据并结束您的脚本。

其次,您的 echo 语句的结果:data: ["insert your data here"] is not valid JSON and you'll not be able to在客户端对其进行解码。

关于javascript - 将 php 作为 "return value"回显到 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23000331/

26 4 0