Webkit(Chrome / Safari)只有AJAX响应错误

时间:2015-04-27 04:42:15

标签: php jquery ajax json webkit

我将一些JSON从一个AJAX请求返回到一个简单的PHP脚本。 IE和IE的一切都运行良好Firefox,但Chrome根本不喜欢这种反应。基本上,我在成功时获得了JSON,但这伴随着错误,因此脚本失败。

jQuery是非常直接的(我已经删除了大部分回调,因为它与问题无关 - 脚本在有或没有它的情况下失败):

$.ajax({
    type: 'GET',
    headers: {'cache-control': 'no-cache'},
    cache: false,
    url: 'ajax/request.php',
    dataType: 'json',
    success: function(a){
        $.each(a, function(b, c){
            var d = $.map(c, function(e){
                return e;
            });

            -- do stuff with results --

        });
    },
    error: function(a, b, c){
        console.log(a);
        console.log(b);
        console.log(c);
     }
});

PHP脚本看起来像这样(我有一个对象执行非常基本的查询,结果编码为JSON格式):

$gallery = new gallery;
$gallery_json = $gallery->select_all($number);
echo json_encode($gallery_json);

控制台(仅限webkit)显示“readyState:4”和“status:200”等,并继续出现以下错误:

parsererror

SyntaxError: Unexpected end of input {stack: "SyntaxError: Unexpected end of input↵    at Object…om/ajax/libs/jquery/1.11.1/jquery.min.js:4:25897)", message: "Unexpected end of input"}
message: "Unexpected end of input"
stack: "SyntaxError: Unexpected end of input↵    at Object.parse (native)↵    at m.parseJSON (http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js:4:15739)↵    at Pc (http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js:4:18120)↵    at x (http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js:4:21525)↵    at XMLHttpRequest.m.ajaxTransport.send.b (http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js:4:25897)"
get stack: function () { [native code] }
arguments: null
caller: null
length: 0
name: ""
prototype: StackTraceGetter
__proto__: function Empty() {}
<function scope>
set stack: function () { [native code] }
arguments: null
caller: null
length: 1
name: ""
prototype: StackTraceSetter
__proto__: function Empty() {}
<function scope>
__proto__: Error
constructor: function SyntaxError() { [native code] }
name: "SyntaxError"
stack: undefined
get stack: function () { [native code] }
arguments: null
caller: null
length: 0
name: ""
prototype: StackTraceGetter
__proto__: function Empty() {}
<function scope>
set stack: function () { [native code] }
arguments: null
caller: null
length: 1
name: ""
prototype: StackTraceSetter
__proto__: function Empty() {}
<function scope>
__proto__: d

(混乱的间距道歉)

JSON与其他浏览器正确回放,因此我确信查询本身没有问题,并且没有报告PHP错误。此外,如果尝试访问返回的JSON的一些内容(例如警告“var d”的某些部分,如“alert(d [2]);”成功回调中的某个地方),那么我可以看到我实际上收到了预期的信息。

我尝试过修改json_encode选项,添加JSON_HEX_QUOTES等,但没有成功。我还尝试向SQL表添加信息,以便不返回任何包含“null”的结果。再一次,没有运气。所以我被卡住了 - 因为错误只出现在webkit控制台中,我只能假设它非常特别关于预期的JSON格式或者什么,但我不知道。并且因为我在我的应用程序的其他地方使用了类似的过程而没有错误,所以我很难确定导致此实例产生此错误的原因。

谢谢你的到来。

2 个答案:

答案 0 :(得分:0)

In most cases your JavaScript is broken in some way. For example missing a } or something like that.

Example given, this will yield "Unexpected end of input" too:

eval('[{"test": 10}') // notice the missing ]
But the root cause of the problems seems to be that the requested JSON url has a Content-Type of text/html which Chrome apparently tries to parse as HTML, which then results in the unexpected end of input due to the fact that the included image tags are being parsed.

Try setting the Content-Type to text/plain I think it should fix the issues.

答案 1 :(得分:0)

嗯,我不确定这对任何人是否有帮助,但也许有人可能能够解释这个奇怪的怪癖。我设法解决了我的问题,但我仍然不确定原因:基本上即使我能够从PHP查询返回有效的JSON页面到应用程序,由于某种原因,某些“$ _SESSION”信息导致了一些问题当JSON回调用于javascript回调时,它会抛出一个解析错误。当我将dataType设置为“text”时,它很好,但是一旦我尝试“$ .parseJSON”,它就会破坏脚本。类似地,将dataType设置为“json”将导致它抛出错误,因此JSON输出中的某些内容导致了毛刺。当我通过AJAX请求传递违规会话信息时,就像在“data:{foo:'bar'}”中一样,一切都很完美 - 相同的数据,只是一种不同的方式来实现它。

我无法理解的是为什么只有WebKit不喜欢以这种方式使用会话信息的方法 - 这对于IE或Firefox来说根本不是问题 - 以及为什么这个实例单独工作(是的,我检查会话信息是否未更改或任何内容)。所以我很想知道以前是否有人遇到过这种奇怪的行为。