【问题标题】:JQuery AJAX inexplicable parseerrorJQuery AJAX莫名其妙的parseerror
【发布时间】:2011-03-16 14:18:44
【问题描述】:

我的 AJAX 脚本都运行良好,并且成功功能正在运行,但脚本仍然会出现错误消息。我已经确定问题是 parseerror。我还没有确定为什么会发生这种情况以及如何阻止它。

AJAX

var dataString = 'title=' + title + '&price=' + price + '&duration=' + duration + '&dives=' + dives + '&hire=' + hire + '&date=' + date + '&currency=' + currency + '&cost=' + cost + '&supplier=' + supplier;

    $.ajax({
            type: 'POST',
             url: '<?php echo $thisposturl?>?catadd',
             data: dataString,
             beforeSend: function() {
                 $('#loadwheel-new').html('<img id="BKloader" src="http://www.divethegap.com/update/z-images/structure/icons/ajax-loader.gif" alt="" width="30" height="30"/>');
                 },
                  error: function() {
                 $('#loadwheel-new').html('lkk');
                 },
                 dataType:'json',
              success: function(data) {
     $('#CollapsiblePanel' + data.CATid).load('<?php echo $thisposturl?> #' + data.CATid);
     ;
} });

PHP

$title = $_POST['title'];
$CATid = $the_post_id;
$date = get_the_time('Y-m-d');
$price = $_POST['price'];
$duration = $_POST['duration'];
$dives = $_POST['dives'];
$hire = $_POST['hire'];
$currency = $_POST['currency'];
$cost = $_POST['cost'];
$supplier = $_POST['supplier'];


echo json_encode( array('title'=>$title, 'CATid'=>$CATid, 'date'=>$date, 'price'=>$price, 'duration'=>$duration, 'dives'=>$dives, 'hire'=>$hire, 'currency'=>$currency, 'cost'=>$cost, 'supplier'=>$supplier));

【问题讨论】:

  • 解析错误发生在服务器端还是客户端?
  • 服务器端。我认为这与 JSON 有关
  • 您从服务器收到的结果是什么?你能用firebug复制粘贴吗?您应该能够查看从服务器收到的响应。
  • 我最近在 ColdFusion 中发生了这种情况:我的问题是附加信息与 JSON 一起返回,所以即使它在创建时是格式正确的 JSON,也无法正确解析. (我打开了页面级调试,因此包含了调试信息。)

标签: javascript php ajax json


【解决方案1】:

在输出 JSON 之前尝试在服务器端使用 ob_clean 清理缓冲区

【讨论】:

    【解决方案2】:

    我在调用 $.getJSON 时遇到了这个问题,但我认为它归结为同一件事,即格式错误的 JSON。我通过将我的 json 解析包装在 JavaScript try/catch 中来解决它。

    【讨论】:

    • 你能详细说明一下 JacaScript 的 try/catch 吗?
    • 当然 - 这不会修复您的 JSON 编码错误,但至少会让您知道出现问题以提醒您查看它,并在出现问题时阻止您的整个页面失败JSON 有问题。我在你的 Ajax 成功回调函数中做了这样的事情:try { var response = $.parseJSON(response); //在这里解析json } catch (err) { //如果json不好,在开发中我可能只是console.log(err) }
    • 嗯,不知道如何让代码显示在 cmets 中,抱歉没有格式化...
    • .. 您可以使用反向刻度线在评论中显示code
    【解决方案3】:

    试过这个吗?

    var dataString = {
        'title': title,
        'price': price,
        'duration': duration,
        'dives': dives,
        'hire': hire,
        'date': date,
        'currency': currency,
        'cost': cost,
        'supplier': supplier
    };
    

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 2012-03-12
      • 2019-02-04
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多