Difference between php echo and return in terms of a jQuery ajax call(就jQuery ajax调用而言,php echo和return之间的区别)
问题描述
我无法让 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之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:就jQuery ajax调用而言,php echo和return之间的区别
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01