AJAX同时返回查询结果和字符串


AJAX returning both query result and string?

我试图创建一个ajax搜索函数,用户要输入搜索条件,然后ajax抓取这些数据并将它们发送到php以从数据库查询结果,然后它返回结果以及存储在字符串中的分页链接。

在javascript文件:

$(document).ready(function(){
   $('.song_filter').change(function(){
      var url = 'listing/ctrl';
      var data {
            title:   $('#title').val();
            author:  $('author').val();
      }
      $.post(url, data)
      .success(result) {
          var jsonObj = $.parseJSON( result );
          output_list(jsonObj); /* fetch out the result */
      }
  });
});

在我的php清单/ctrl中,我已经准备好返回查询结果和分页链接。我没有问题,只返回查询结果本身,但我不知道如何返回结果和链接。

$result = $this->pagination->get_result();
$page_nav = $this->pagination->page_nav(); // <li class="page">2</li><li class="page">3</li>
echo json_encode($result, JSON_UNESCAPED_UNICODE);

您可以将链接附加到json对象,如下所示:

$result = $this->pagination->get_result();
$page_nav = $this->pagination->page_nav();
// create an object to store the result and the link
$json = new stdClass();
$json->link = $page_nav;
$json->data = $result;
echo json_encode($json, JSON_UNESCAPED_UNICODE);

在你的ajax成功

.success(result) {
      var jsonObj = $.parseJSON( result );
      jsonObj[0].link /* Here comes the link */
      jsonObj[0].data;
  }