gpt4 book ai didi

php - JSON编码的数组在什么情况下可以变成字符串?

转载 作者:行者123 更新时间:2023-12-01 07:16:54 24 4
gpt4 key购买 nike

我有一些与此类似的 PHP 5 代码:

$result = myFunction(...);  // return false, or doit action
$reply = array();
if ($result) {
$reply['doit'] = $result;
$reply['status'] = "a status html string";
} else {
$reply['content'] = "Some html text";
$reply['menu'] = "Some other html text";
$reply['status'] = "a different status html string";
}
return $reply;

调用者包含片段

$reply = somefunction();
echo json_encode($reply);

然后将此回复发送到客户端,jquery 将其传递给我的函数

function handleReply(reply) {
if (reply.doit) {
handle action
}
if (reply.content) document.getElementById('content').innerHTML=reply.content;
if (reply.menu) document.getElementById('menu').innerHTML=reply.menu;
if (reply.status) document.getElementById('status').innerHTML=reply.status;
}

我一直困扰的是,当执行if语句的doit分支时,($result是一个字符串)jquery给我的回复是一个字符串。当内容/菜单/状态侧被获取($result 为 false)时,reply 是一个对象。

我已向数组添加了第二个索引,结果是相同的。尽管所有字符串都是 ASCII,但我尝试通过 UTF8_encode 传递它们。我已将“doit”索引的名称从“action”更改为以防触发 jquery 中的某些行为。

需要明确的是,错误时的回复是(例如)。

"{"doit":"obj=session&act=show&ID=3","status":"<p>Nic: Ian<br\/>Reseller: Coachmaster.co.uk<br\/>Status: SysAdmin <\/p>"}"

这是一个字符串。我预计:

{"doit":"obj=session&act=show&ID=3","status":"<p>Nic: Ian<br\/>Reseller: Coachmaster.co.uk<br\/>Status: SysAdmin <\/p>"}

这是一个对象/数组。这也是我的记录所显示的得到回应的内容。

我在 Windows 7 和 Apache 下使用 php5.4.3,在 linux 和 nginx 下使用 php5.3.10,结果相同。 jquery 的版本都是 v1.7.2。还加载了 jQuery UI - v1.10.3 - 2013-07-02。

如果这是 jquery 中的一个错误,那么它是一个非常奇怪的错误。我该如何证明呢?

最佳答案

我认为你依赖于 jQuery 自动检测。尝试:

header('Content-Type: application/json');

关于php - JSON编码的数组在什么情况下可以变成字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17949297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com