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之间的区别


基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01