AJAX返回查询结果和字符串?

时间:2014-05-28 21:27:50

标签: php jquery ajax string pagination

我试图创建一个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 listing / 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);

1 个答案:

答案 0 :(得分:1)

您可以将链接附加到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;
  }