【问题标题】:How to get more than 1 result through ajax response with json如何通过带有 json 的 ajax 响应获得超过 1 个结果
【发布时间】:2014-12-05 09:48:02
【问题描述】:

我正在制作一个消息传递系统,我必须获取消息,这是我的代码

我的 ajax 代码来获取结果-

function fetch_conversation() {
    $.ajax({
        type: 'post',
        url: 'message_function.php',
        data: {
            fetch_conversation:'conversation'
        },
        success: function (response) {
            var r = $.parseJSON(response);
            alert(r.nno);
            alert(r.mmessage_from);
        }
    });
}

这是我的 php 代码,它获取数据并将其作为响应发送到 ajax-

if(isset($_POST['fetch_conversation'])) {
    $select_message=mysql_query("select no,message_from,sent_time,message_body from    messages where message_to='1'  and sent_time > '2014-12-02 17:43:35' union select   no,message_to,sent_time,message_body from messages where message_from='1' and sent_time >   '2014-12-02 17:43:35' ")or die(mysql_error());

    while($row=mysql_fetch_array($select_message)) {
        $nno= $row['no'];
        echo json_encode($nno);
        $mmessage_from=$row['message_from'];
        echo json_encode($mmessage_from);
    }

    exit();
}

我使用的查询获取超过 1 行,我的问题是 json 无法在 fetch 对话函数中返回值,如果查询仅获取 1 行,它 json 会显示所有值并且所有警报都是可见的。如何使我的获取对话功能可以获取超过 1 行并显示警报,请任何帮助将不胜感激,也请尝试提供跨浏览器的代码,谢谢....

【问题讨论】:

  • $mmessage_from[] 并将 echo 放在 wile 循环之外
  • 我这样做了,但没有任何效果
  • 你做得不对。将两个 echo 放在循环之外,在分配时 $nno 将变为 $nno[] 并且 $mmessage_from 将变为 $mmessage_from[] 当您在循环之外(退出之前)您需要使用 echo json_encode($nno 打印它们[]);并回显 json_encode($mmessage_from[]);
  • 就像@MarcoMura 说的那样,或者只使用一个像$out = array("nno" => array(),"message_form" => array()) 这样的数组,并且在循环之后只使用json_encode
  • 是的,我按照你说的那样做,不显示警报

标签: php jquery mysql ajax json


【解决方案1】:
**in the mesage_function.php file**

if(isset($_POST['fetch_conversation'])) {
    $select_message=mysql_query("select no,message_from,sent_time,message_body from    messages where message_to='1'  and sent_time > '2014-12-02 17:43:35' union select   no,message_to,sent_time,message_body from messages where message_from='1' and sent_time >   '2014-12-02 17:43:35' ")or die(mysql_error());
    $array = array();
    $i = 0;
    while($row=mysql_fetch_array($select_message)) {
        $array[$i]['no']= $row['no'];
        $array[$i]['mmessage_from']=$row['message_from'];
        $i++;
   }
   echo json_encode($array);
}   

**and in the ajax sucess alert only response and check log**

**try this, i hope this help.**

在您的成功响应中使用此代码.. 将此代码放在 parsejson 之后 添加您的表 id 在这里。

#add_your_id_here

function fetch_conversation()
 {
      $.ajax({
        type: 'post',
        url: 'message_function.php',
        data: {
            fetch_conversation:'conversation'
        },
        success: function (response) {

            var r = JSON.parse(response);
                var text = "";
                var x;
                for (x in r) {
                    $('#add_your_id_here').append('<tr><td>'+r[x]['mmessage_from']+'</td><td>'+r[x]['no']+'</td></tr>');

                }
            }
        });
 }

【讨论】:

  • 是的,它会提醒我所有值,但如何像我在 json 中提取的值一样提取我放入元素中的值
  • 你能给我举个例子你需要哪个输出吗?
  • 就像我想在 ajax 函数中获取 'no' 中的所有值和 'message_from' 的值,这样我就可以将 innerHTML 赋予我的元素
  • 抱歉您的代码无法正常工作,但感谢您的帮助
  • 没有错误代码没有在我的具有 id 消息的

    元素中显示任何结果

【解决方案2】:

你什么时候得到所有结果:

console.log(r);

你可以试试

$.each(r, function(k, row) {
    alert(row.nno);
    alert(row.mmessage_from);
}

【讨论】:

    【解决方案3】:
     if(isset($_POST['fetch_conversation']))
     {
        $select_message = mysql_query("SELECT no,message_from,sent_time,message_body 
                                       FROM  messages WHERE message_to='1'  
                                       AND sent_time > '2014-12-02 17:43:35' 
                                       UNION 
                                       SELECT no,message_to,sent_time,message_body 
                                       FROM messages WHERE message_from='1' AND sent_time > '2014-12-02 17:43:35' ") or die(mysql_error());
         // create a result array to send response
         $result = array();   
         while($row=mysql_fetch_array($select_message))
         {
            $_temp = array();
            $_temp['no'] = $row['no'];
            $_temp['message_from'] = $row['message_from'];
            $result[] = $_temp;
            unset($_temp);
         }
         @header("Content-Type:text/json");
         echo json_encode($result);
         exit;
     }
    

    请尝试使用上面的代码。这将为您提供由多个数组记录创建的 json。在您的 html 代码中,请按照@Ferret 的建议进行操作。

    【讨论】:

      【解决方案4】:

      试试这个,

         function fetch_conversation()
         {
        $.ajax({
          type: 'post',
          dataType: 'json',
          url: 'message_function.php',
          data: {
          fetch_conversation:'conversation'
          },
          success: function (response) {
      
          var msg_no=response['no'];
          var msg_from=response['message_from'];
      
          alert(msg_no);
          alert(msg_from);
          }
          });
        }
      

      还有你的 php

      if(isset($_POST['fetch_conversation']))
       {
        $select_message=mysql_query("select no,message_from,sent_time,message_body from       messages where message_to='1'  and sent_time > '2014-12-02 17:43:35' union select   no,message_to,sent_time,message_body from messages where message_from='1' and sent_time >   '2014-12-02 17:43:35' ")or die(mysql_error());
       while($row=mysql_fetch_array($select_message))
       {
        $nno[]= $row['no'];
      
        $mmessage_from[]=$row['message_from'];
      
        }
        $arr['no']=$nno;
        $arr['message_from']=$mmessage_from;
      
         echo json_encode($arr);
       exit();
       }
      

      【讨论】:

      • 它给了我未定义的值警报
      • 在 ajax 上添加 dataType=json, 然后尝试
      猜你喜欢
      • 2014-12-10
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多