AJAX GET 函数出现问题
我一直在互联网上寻找 AJAX 函数的示例,该函数从数据库获取最新消息,我一直在研究代码,但我陷入困境,目前我不知道如何从 php 文件获取数据到 JQuery 中的变量。听起来很混乱,也许看看我的代码会有帮助。
非常感谢任何帮助。
JQuery代码
$.ajax({
type: "GET", url: "include/process.php",
data:{
getLatestActivity: "true",
toUser: "4"
},
success: function(data){
alert("data: "+data);
}
});
php处理请求
function getLatestActivity(){
global $session;
$data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));
if($data){//successful
return $data;
}
}
data函数只打印出“data:”,提前致谢
I have been looking around the internet for examples of an AJAX function which gets the latest messages from a database, I have been playing around with the code but I am stuck, currently I don't know how I get the data from the php file to a variable in JQuery. sounds confusing maybe looking at my code will help.
any help is much appreciated.
JQuery code
$.ajax({
type: "GET", url: "include/process.php",
data:{
getLatestActivity: "true",
toUser: "4"
},
success: function(data){
alert("data: "+data);
}
});
php processing request
function getLatestActivity(){
global $session;
$data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));
if($data){//successful
return $data;
}
}
The data function only prints out "data:", Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了将数据从 PHP 传递到 Javascript,您需要
echo
您尝试返回的数据的字符串/json 表示形式。注意:echo json_encode($data);
In order to pass data from PHP to Javascript you need to
echo
a string/json representation of what ever data you are trying to return. Note:echo json_encode($data);
不确定是否有比这更多的代码,但如果您在使用 Ajax 调用 PHP 文件时没有实际运行 PHP 文件,那么您还必须运行该函数:
如果它是唯一的函数,您可以删除该函数调用并运行直接编码,根据数据的格式,您可能必须像其他答案所说的那样对其进行解码。
Not sure if there is more code than this, but if not your actually running the PHP file when calling it with Ajax, and you will have to run the function aswell:
If it's the only function you could just remove the function call and run the code directly, and depending on the format of the data you may have to decode it like the other answers are saying.