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 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思