问题描述
我无法让 jQuery Ajax 调用的成功函数正常工作,有人向我指出原因是我的 PHP 函数在我应该使用 echo $result 时使用了 return $result.
I was having trouble getting a jQuery Ajax call's success function to work properly and it was pointed out to me that the reason was that my PHP function was using return $result when I should be using echo $result.
将 Ajax 调用的 PHP 函数从return $result"更改为echo $result"解决了这个问题,但为什么呢?关于 PHP 脚本中两者之间的区别(返回和回显)有很多解释,但是当将该值发送到 Ajax 调用时它们有何不同?
Changing the PHP function that the Ajax called from "return $result" to "echo $result" fixed the problem, but why? There's loads of explanations as to the difference between the two (return and echo) in terms of PHP scripts, but how do they differ when sending that value to an Ajax call?
推荐答案
好吧,ajax 调用从服务器读取响应,并且该响应必须呈现为某种类型的可读数据,例如 application/json
或 text/html
.
Well, the ajax call reads the response from the server, and that response must be rendered as some type of readable data, such as application/json
or text/html
.
为了写入该数据,您需要使用 PHP 从服务器 echo
它.
In order to write that data, you need to echo
it from the server with PHP.
return 语句不写入数据,它只是在服务器级别返回.
The return statement doesn't write data, it simply returns at the server level.
这篇关于就jQuery ajax调用而言,php echo和return之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!