duancuan6466 2012-04-04 13:50
浏览 69
已采纳

json_encode与PHP 5.1.6和jQuery

PHP5.1.6 has no json_encode(), so I wanted to use function found on json_encode documentation. I am trying to use output of this function with ajax request:

fetchArticles: function( e ) {
      $.ajax({
        url: 'article.php',
        type: 'POST',
        data:  { id: $(this).data( 'id_prod' ) },
        dataType: 'json',
        success: function( results ) {
          console.log('finished');
          console.log(results);
        }
      });

In article.php I have at this time:

if ( isset($_POST['id']) ) {
 connect();
 $articles = get_articles( $_POST['id'] );
 echo json_encode( $articles ); return;
}

The problem is with returning results to JS console:

  • if there is no results, empty array is printed,
  • if there is one results, proper object is printed,
  • but when there is more than one result nothing is being printed to console, not even word finished.

I can see in HTTP headers and responses that proper data is returned, but it is not printed to console. Could you help me with solving this problem?

  • 写回答

1条回答 默认 最新

  • dongmibeng5885 2012-04-04 13:53
    关注

    When the json file can't be converted it throws parsererror exception, so try this:

      $.ajax({
        url: 'article.php',
        type: 'POST',
        data:  { id: $(this).data( 'id_prod' ) },
        dataType: 'json',
        success: function( results ) {
          console.log('finished');
          console.log(results);
        },
        error: function(jqXHR, textStatus, errorThrown) { 
          console.debug(jqXHR, textStatus, errorThrown); 
        }
      });
    

    It will output your error when the JSON is invalid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看