weixin_33698043 2016-04-08 07:11 采纳率: 0%
浏览 33

AJAX JSON解析器错误PHP

Here is my code. I keep getting a json parser error. The ajax never goes into done. Help!

<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');
    }
?>

UPDATE: Error message shows this in the console:

"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条回答 默认 最新

  • weixin_33724046 2016-04-08 08:03
    关注

    You need to end-up your ajax response call with exit();

    if ($action == "load") {
              echo json_encode($red);
              exit(); 
    }
    

    and print will let you formatted output which not required, I think.

    so just echo also been working.

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵