dongyoufo5672 2014-04-26 18:01
浏览 334
已采纳

AJAX请求不返回数据

I am trying to run a script in the background of my PHP pages that will check if new messages have been added to the database and notify the user.

I used AJAX to call a file with the code and the setTimeout() to call it every 10 seconds.

The problem is that the ajax is not returning any data. I tried just putting "return: 1;" but i still get an blank alert box.

PHP:

require('connect.php');
require_once("inc/init.php");

$check_Mail_footer = $con->prepare("SELECT * FROM mail WHERE `to` = ? AND `deleted` = ? AND `read` = ?");
$check_Mail_footer->execute(array($user_id,0,0)); 
$result = $check_Mail_footer->fetch(PDO::FETCH_OBJ); 

return array($result->from, $result->message_id);

Javascript:

        function checkNewMail(){

        $.ajax({
              url: 'newMessageCheck.php',
              success: function(info) 
              {
                  alert(info);

              }, error: function()
              {
                  alert('something went wrong');
              }
        });

        }


        setTimeout( checkNewMail() ,10000);
  • 写回答

2条回答 默认 最新

  • douju7765 2014-04-26 18:05
    关注

    You need to echo out the data you want to return to Javascript - you can't return it

    For your array, you probably want to json_encode it first so write:-

    echo json_encode(array($result->from, $result->message_id));
    

    to replace:-

    return array($result->from, $result->message_id);
    

    and then change your $.ajax call to automatically parse the JSON

    $.ajax({
              type: "GET",
              url: 'newMessageCheck.php',
              dataType:'json',
              success: function(info) 
              {
                  alert(info[0]);
    
              }, error: function()
              {
                  alert('something went wrong');
              }
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型