【问题标题】:jquery ajax "SyntaxError: Unexpected end of input" on valid JSON有效JSON上的jquery ajax“SyntaxError:输入意外结束”
【发布时间】:2015-04-01 15:26:18
【问题描述】:

我的 PHP 返回的 JSON 是:

{"success":0,"message":"Error: No entityId pass!"}

我的 javascript 仍然告诉我“SyntaxError: Unexpected end of input”。

PHP:

    ...

    //check if an image id was passed for removal in the POST data
    if ( isset($_POST["entityId"])) {
        $entityId = $_POST["entityId"];
    }
    else {
        //setup response json
        $resp = array();
        $resp['success'] = 0;
        $resp['message'] = "Error: No entityId passed!";

        header('Content-Type: application/json');
        echo json_encode($resp);
    }

    ...

JS:

             // send xhr request
             $.ajax({
                 dataType: 'json',
                 type: $theForm.attr('method'),
                 url: $theForm.attr('action'),
                 data: $theForm.serialize(),
                 success: function(data) {
                     //backend returns a json with variable success either true or false.
                     resp = data;
                     if(resp.success == 1) {
                        //render gallery anew
                     }
                     else {
                        console.log(data);
                        if (data.message) {
                            $(self).find('.VanillaGallery-overlayContentWrapper').html(data.message);       
                        }
                        else {
                            $(self).find('.VanillaGallery-overlayContentWrapper').html('Oops! Något gick fel, felmeddelande saknas dock.');       
                        }
                     }

                 },
                 error: function(xhr, status, text) {
                     $(self).find('.VanillaGallery-overlayContentWrapper').html('Oops! Något gick fel...<br />'+text);
                 }
             });

很奇怪。我的标头会有所不同,具体取决于请求是通过 ajax 还是作为直接在浏览器中的普通单独请求(而不是通过 ajax)发出:

通过 ajax 看起来像这样的标题:

Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 01 Apr 2015 17:48:26 GMT
Keep-Alive:timeout=5, max=97
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zc DAV/2 PHP/5.5.3
X-Pad:avoid browser bug
X-Powered-By:PHP/5.5.3

直接从浏览器地址栏通过这个:

Connection:Keep-Alive
Content-Length:52
Content-Type:application/json
Date:Wed, 01 Apr 2015 17:42:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zc DAV/2 PHP/5.5.3
X-Powered-By:PHP/5.5.3

【问题讨论】:

  • 您是否检查了开发人员工具以验证 HTTP 响应是否真的像您认为的那样?
  • 可能是我的本地主机(MAC 上的 Apache,MAMP)未配置为提供正确的 JSON 响应
  • 我在 Chrome 中的网络日志显示 'application/json' 这是否意味着服务器 (localhost) 正在提供正确的 json 响应?
  • 我很确定 jQuery 不会关心内容类型标题的内容。如果你告诉它它是 JSON,它会尝试解析它。开发人员工具中的网络选项卡将让您看到您的服务器是否真的发回了您认为的响应。换句话说,您将看到 实际 响应内容。
  • 我添加了回复。为什么它在一个实例中被解释为 application/json,而在通过 ajax 时被解释为 text/html?

标签: javascript jquery ajax syntax


【解决方案1】:

好的,所以一旦你用新的眼光看它就很明显......在ajax请求中传递了一个entityId(从示例代码中不明显,因为没有显示表单数据......)因此第一个上面 PHP 代码中的 IF 条件计算为真。在那个子句中,没有输出,没有任何形式的回声。这就是为什么我得到“输入意外结束”的原因。

至于通过直接在浏览器地址栏中运行来测试它,这样 PHP 当然会落在我上面 PHP 代码的 else-bracket 中,并且实际上会给出响应,因为根本没有 POST 数据在做就这样……

很抱歉占用您的时间,有时实在是太累了……

【讨论】:

    【解决方案2】:

    SyntaxError: Unexpected end of input 它通常与 JS(甚至 PHP)中的一些错误/错字有关,顺便说一句,您的代码独立运行正常。您应该检查其余部分...也许您在某处遗漏了括号或分号。

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      相关资源
      最近更新 更多