AJAX GET 函数出现问题

发布于 2024-12-28 11:07:38 字数 693 浏览 0 评论 0原文

我一直在互联网上寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2025-01-04 11:07:38

为了将数据从 PHP 传递到 Javascript,您需要echo您尝试返回的数据的字符串/json 表示形式。注意:echo json_encode($data);

function getLatestActivity(){
   global $session;

   $data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));

   if($data){//successful
      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);

function getLatestActivity(){
   global $session;

   $data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));

   if($data){//successful
      echo json_encode($data);
   }
}
戏蝶舞 2025-01-04 11:07:38

不确定是否有比这更多的代码,但如果您在使用 Ajax 调用 PHP 文件时没有实际运行 PHP 文件,那么您还必须运行该函数:

function getLatestActivity(){
   global $session;

   $data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));

   if($data){//successful
      return $data;
   }
}

$mydata = getLatestActivity();
echo $mydata;

如果它是唯一的函数,您可以删除该函数调用并运行直接编码,根据数据的格式,您可能必须像其他答案所说的那样对其进行解码。

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:

function getLatestActivity(){
   global $session;

   $data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));

   if($data){//successful
      return $data;
   }
}

$mydata = getLatestActivity();
echo $mydata;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文