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条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿