AJAX JSON Parser错误PHP

时间:2016-04-08 07:11:04

标签: jquery json ajax

这是我的代码。我一直得到一个json解析器错误。 ajax永远不会完成。救命啊!

<input type="submit" class="button" name="insert" value="load"/>
<div id="wines">
    <!-- Javascript will print data in here when we have finished the page -->    
</div>
jQuery(document).ready(function() {
    var $ = jQuery;
    var baseUrl = [location.protocol, '//', location.host, location.pathname].join('');

    $('.button').click(function(){ // This event fires when a button is clicked
        var button = $(this).val();
        $.ajax({ // ajax call starts
            url: baseUrl, // JQuery loads serverside.php
            data: 'action=' + $(this).val(), // Send value of the clicked button
            dataType: 'json', // Choosing a JSON datatype
        }).done(function(data) { // Variable data contains the data we get from serverside
            console.log("j");      
        }).fail(function(data,error) { 
            console.log(error);  
       })
    });
});
<?php
    $action = req('action');
    // Red wine table
    $red = array('Chianti', 'Barolo', 'Pinot Noir');
    $white = array('Chardonnay', 'Cava', 'Chablis');

    // Combine red and white tables into one multidimensional table

    $winetable = array(
      "red" => $red,
      "white" => $white,
    );

    // Finally depending on the button value, JSON encode our winetable and print it
    if ($action == "load") {
          print json_encode($red);
          header('Content-Type: application/json');
    }
?>

更新: 错误消息在控制台中显示:

"Initializing System Events for WUH..." common_admin.js:22
"["Chianti","Barolo","Pinot Noir"]
<input type="submit" class="button" name="insert" value="load"/>
<div id="wines">
  <!-- Javascript will print data in here when we have finished the page -->
</div>


<script type="text/javascript">
jQuery(document).ready(function() {

    var $ = jQuery;
    var baseUrl = [location.protocol, '//', location.host, location.pathname].join('');

  $('.button').click(function(){ // This event fires when a button is clicked
    var button = $(this).val();
    $.ajax({ // ajax call starts
      url: baseUrl, // JQuery loads serverside.php
      data: 'action=' + $(this).val(), // Send value of the clicked button
      dataType: 'json', // Choosing a JSON datatype
    })
    .done(function(data) { // Variable data contains the data we get from serverside
      console.log("j");      
    })
    .fail(function(data,error) { 
      console.log(data.responseText+error);  
     })
  });
});
</script>parsererror"

2 个答案:

答案 0 :(得分:3)

首先,您应该在发送任何正文之前打印标题,否则您可能会遇到经典标题错误:

  

警告:无法修改标头信息 - 已由(...)

发送的标头

为此,您应该在headers()前移动print json_encode(...)电话,如下所示:

// Finally depending on the button value, JSON encode our winetable and print it
if ($action == "load") {
      header('Content-Type: application/json');
      print json_encode($red);
}

接下来,正如您在打印JSON后可能会运行其他指令一样,您应该像@PranavBhaat所说的那样,在脚本末尾添加dieexit语句,这样您就可以确定没有其他内容输出,并且您的AJAX调用正确解析了JSON。

答案 1 :(得分:0)

&lt; p&gt;您需要使用&lt; code&gt; exit();&lt; / code&gt;&lt; / p&gt;结束您的ajax响应通话 &lt; pre&gt;&lt; code&gt; if($ action ==“load”){           echo json_encode($ red);           出口(); } &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;并打印将为您提供不需要的格式化输出,我认为。&lt; / p&gt; &lt; p&gt;所以只有echo也一直在工作。&lt; / p&gt;