2015-04-27 90 views
0

我從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格式非常特別,但我不知道。而且因爲我在應用程序的其他地方使用了類似的過程而沒有發生錯誤,所以我很難確定是什麼導致此實例產生此錯誤。

非常感謝。

+0

作爲一個快速的後續行動,我只是嘗試刪除「成功:功能(一){...等}」 - 這就是說沒有成功回調 - 腳本同樣失敗。 – mimewear

回答

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. 
+0

謝謝Arpita。我已經檢查了好幾次javascript,並且看不到任何會導致它斷開的錯誤,並且我希望它會在其他瀏覽器中崩潰。我也嘗試添加以下標題:header(「Content-Type:text/plain」); – mimewear

+0

這裏是查詢的JSON輸出:[{「num」:「35」,「user」:「11199394」,「title」:「Image Title」,「category」:「fashion」,「date」 1428511556" , 「文件」: 「photo128」, 「類型」: 「JPG」, 「大小」: 「115082」, 「寬度」: 「800」, 「高度」: 「590」, 「等級」:NULL,」註釋「:」Fusce eget felis dui.Sed ullamcorper luctus orci at dapibus。Suspendisse non pretium lacus.Ut a nulla mauris.Vivamus placerat libero in augue elementum dapibus.Donec nec suscipit sem。「,」comments「:null,」score「 :NULL, 「包」:NULL, 「貓-NUM」: 「10」}] – mimewear

0

嗯,我不確定這是否對任何人都有幫助,但也許有人可能能夠解釋這種奇怪的怪癖。我設法解決了我的問題,但我仍然不確定原因:基本上,即使我能夠從PHP查詢返回有效的JSON頁面到應用程序,出於某種原因,某些「$ _SESSION」信息導致了某些內容JSON在返回用於JavaScript回調時拋出一個解析錯誤。當我將dataType設置爲「text」時,它很好,但只要我嘗試「$ .parseJSON」就會破壞腳本。同樣,將dataType設置爲「json」會導致它引發錯誤,所以JSON輸出中的某些內容導致了毛刺。當我通過AJAX請求傳遞違規會話信息時,例如在「data:{foo:'bar'}」中,一切工作都很完美 - 相同的數據,只是以不同的方式獲得它。

我無法理解的是,爲什麼只有WebKit不喜歡我這種以這種方式使用會話信息的方法 - 對於IE或Firefox來說這完全不是問題 - 以及爲什麼這個實例單獨以這種方式工作(是的,我檢查了會話信息沒有被更改或任何東西)。所以我很想知道是否有人以前遇到過這種奇怪的行爲。