【问题标题】:jQuery AJAX 200 status, yet mysterious syntax errorjQuery AJAX 200 状态,但神秘的语法错误
【发布时间】:2012-12-13 18:23:39
【问题描述】:

我在使用 jQuery AJAX 时返回了状态 200。但是,我也从某个地方收到语法错误。我像这样发布到 PHP:

function submit_order(orderInformation) {
    $.ajax({
        type: 'post',
        url: 'queries/submit_order.php?<?=time();?>',
        data: 'orderInformation=' + JSON.stringify(orderInformation),
        dataType: 'json',
        success: function (returnedData) {
            console.log(returnedData);
            $('#content_container').fadeOut(340, function () {
                var new_content = $('#content_container').clone(false);
                $('#content_container').remove();
                new_content.css('display', 'none');
                new_content.children().remove();

                new_content.appendTo('body');
                $('#content_container').vkTemplate('templates/confirm_template.tmpl?<?=time()?>', returnedData, function (el, data, context) {
                console.log('success'); 

                    $('#content_container').fadeIn(340);
                });
            });
        },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
      }
    });
}

我的 PHP 代码非常简单:

$order_information = json_decode($json_str, true);

//go through the array and make an email out of it
//add a few elements to the array
//send the email

//send back a json string with the added elements
echo json_encode($order_information);

但我明白了:

奇怪的是,如果我将 JSON 字符串从 console.log(JSON.stringify(orderInformation)) 复制粘贴到 PHP 页面中:

$json_str = '{"sector_0":{"file":[],"sector_info":{"sector_label":"NIO","purchase_order":"test","proof":false},"lines":{"line_0":{"description":"test","quantity":"2","productId":"1","addressId":"20","shipViaId":"1","notes":false}}}} ';

一切正常。这是什么错误?错误中看到的这个&lt; 可能来自哪里?

谢谢

【问题讨论】:

  • 200 不是错误。这是来自网络服务器的 OK 响应。
  • 您是否尝试在浏览器中打开queries/submit_order.php?&lt;?=time();?&gt; 或使用firebug 检查服务器究竟返回了什么?貌似有语法错误什么的,返回html
  • @Codeguy007 那么为什么将其打印到error 回调触发的日志中?谢谢!
  • =time();?> 肯定不行,因为你不能运行php客户端。
  • 那么检查服务器返回的内容了吗?原始响应。

标签: javascript php jquery ajax json


【解决方案1】:

触发并记录的是您的错误处理程序:

  • xhr.status (200)
  • throwError(语法错误)

请注意,$.ajaxdataType: json 将触发错误处理程序,即使服务器返回 200 OK 但响应是无效的 JSON。语法错误不在您的 JavaScript 代码中,而是在 JSON 中。确定 &lt; 的来源并确保您的 PHP 脚本发送有效的 JSON。

提示:打开控制台并查看网络选项卡;所有 XHR 连同标题和正文都记录在那里。

【讨论】:

  • 谢谢。我发现语法错误来自 php 打印出&lt;br /&gt; &lt;b&gt;Warning&lt;/b&gt;: Invalid argument supplied for foreach() in &lt;b&gt;/public_html/bch/queries/submit_order.php&lt;/b&gt; on line &lt;b&gt;54&lt;/b&gt;&lt;br /&gt; 所以,因此是&lt;。我不明白 foreach 循环有什么问题,但是 foreach ( $order_information as $sector_index =&gt; &amp;$sector_value ){
  • 那么这是一个 PHP 问题 :) 您可以将其作为另一个问题发布或忍者编辑您的问题。 PS:$order_information 很可能不是数组。
  • PHP 在字符串中添加斜杠:/ stripslashes 解决了这个问题。它总是这样做吗?感谢您的帮助!
【解决方案2】:

200 - 是服务器 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 的 Ok 响应

您的响应服务器中存在语法错误,返回无效的 json

由于您的 PHP 代码接缝良好,因此肯定还有其他问题。语法错误或您的框架返回包装在 html 中的 json ...

使用适当的工具查看服务器返回的内容。 (firefox 上的 firebug/chrome 上的开发者工具)

在您的图像中,您会看到 0: "&lt;" 这意味着返回的字符串以 &lt; 开头 - 这意味着返回的是 html。

看起来您使用的是 chrome。转到 chrome 中的“网络”标签,您应该能够看到对您的请求的原始响应。

所以这是一个php错误:

$sector_index 不可迭代。你能不能 var_dump 看看。它是什么?

【讨论】:

  • 现在检查它是否在您的 $sector_index 中。由于 php 不知道如何遍历它
  • PHP 出于某种原因在它接收的 JSON 字符串中添加了斜杠。我不知道它是否总是这样,但 stripslashes 似乎已经修复了它。谢谢你带我完成这个:)
【解决方案3】:

看起来&lt;?=time()?&gt; 没有得到处理。在发布到 URL 以进行验证之前提醒 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 2012-04-04
    • 2015-10-13
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2013-02-18
    • 2022-01-08
    相关资源
    最近更新 更多