我從AJAX請求返回一些JSON到簡單的PHP腳本。 IEFirefox一切正常,但Chrome根本不喜歡響應。基本上,我獲得成功的JSON,但這是伴隨着一個錯誤,所以腳本失敗。僅Webkit(Chrome/Safari)AJAX響應錯誤
的jQuery是非常straigt進(我已刪除大部分的回調,因爲它是不相關的問題 - 腳本失敗或沒有它):
$.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」和「狀態: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格式非常特別,但我不知道。而且因爲我在應用程序的其他地方使用了類似的過程而沒有發生錯誤,所以我很難確定是什麼導致此實例產生此錯誤。
非常感謝。
作爲一個快速的後續行動,我只是嘗試刪除「成功:功能(一){...等}」 - 這就是說沒有成功回調 - 腳本同樣失敗。 – mimewear