通过AJAX返回PHP数组而不使用json_encode

时间:2022-04-30 21:29:35

Just a little quirk I've come accross and I think I must be missing something so would like some clarification to further my understanding of what's going on with my code.

我只是有点怪癖,我想我必须遗漏一些东西,所以想要澄清一下,以进一步了解我的代码发生了什么。

I post a form via jQuery AJAX, the form is processed by PHP and in the PHP I have the line

我通过jQuery AJAX发布表单,表单由PHP处理,在PHP中我有行

return $status;

$status is an array containing several values i.e $status['username'] = 'admin'

$ status是一个包含多个值的数组,即$ status ['username'] ='admin'

The AJAX datatype is set to 'JSON' but I am not using echo json_encode($status); in my PHP but everything is still working and my $status array is processed by jQuery upon AJAX success. Why is this? I thought json_encode would be required but it seems it isn't.

AJAX数据类型设置为'JSON',但我没有使用echo json_encode($ status);在我的PHP中但是一切仍然有效,我的$ status数组在AJAX成功时由jQuery处理。为什么是这样?我认为json_encode是必需的,但似乎不是。

2 个解决方案

#1


0  

json_encode use but out of this function that you see the

json_encode使用,但你看到的这个功能

  return $status;

on it for understand it you can see developer tools in browser then find ajax request, see content response . for find json_encode search for location of calling this function.

在它上面了解它你可以在浏览器中看到开发人员工具然后找到ajax请求,看看内容响应。 for find json_encode搜索调用此函数的位置。

#2


0  

Apologies I had made a daft mistake, my function was indeed returning $status, but it was returning it to another function which performed the json_encode. Mystery solved :) Thanks.

道歉我犯了一个愚蠢的错误,我的函数确实返回$ status,但是它将它返回到执行json_encode的另一个函数。神秘解决了:)谢谢。

#1


0  

json_encode use but out of this function that you see the

json_encode使用,但你看到的这个功能

  return $status;

on it for understand it you can see developer tools in browser then find ajax request, see content response . for find json_encode search for location of calling this function.

在它上面了解它你可以在浏览器中看到开发人员工具然后找到ajax请求,看看内容响应。 for find json_encode搜索调用此函数的位置。

#2


0  

Apologies I had made a daft mistake, my function was indeed returning $status, but it was returning it to another function which performed the json_encode. Mystery solved :) Thanks.

道歉我犯了一个愚蠢的错误,我的函数确实返回$ status,但是它将它返回到执行json_encode的另一个函数。神秘解决了:)谢谢。