2012-10-15 22 views
1

我試圖僅通過Ajax和Internet Explorer獲取JSON數據時遇到問題。從PHP + Ajax獲取JSON數據+ Zend僅在IE中工作

我使用這個jQuery代碼:

$.ajax({ 
    type: "GET", 
    url: "./ajaxglobal/chargementcommandes", 
    cache: false, 
    dataType: "json", 
    contentType: "application/json", 
    success: function(data) { 
     alert("Success : "+data); 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     alert("Error : "+textStatus+"/"+errorThrown); 
    } 
}); 

隨着 「chargementcommandes」 是一個PHP/Zend的頁面。

「chargementcommandes」,簡化了測試的PHP代碼,如下:

echo '[ 
     { "data" : "A node", "children" : [ { "data" : "Only child", "state" : "closed" } ], "state" : "open" }, 
     "Ajax node" 
]'; 

一切都在Firefox和Chrome工作正常(我得到預期的數據),但在Internet Explorer(在測試IE8和IE9),我從警報中獲得「成功:未定義」。所以Ajax調用不會返回任何內容。我嘗試了很多東西,但是我沒有成功擺脫這個問題。

任何幫助將不勝感激。 在此先感謝!

編輯:在jQuery代碼中添加了類型,緩存和內容類型。仍然「成功:未定義」響應...

+0

用什麼'內容Type'服務器響應? – raina77ow

+0

我希望這個解決方案可以幫助你(它適用於我)http://stackoverflow.com/questions/2037412/ajax-call-from-jquery-works-in-firefox-but-not-in-ie8 – juanchopx2

+0

我'已經編輯我的Ajax代碼,以確保內容類型是確定的,並且沒有使用高速緩存: '$阿賈克斯({ \t類型:「GET」, \t網址:「./ajaxglobal/chargementcommandes」 \t緩存:假, \t數據類型: 「JSON」, \t的contentType: 「應用程序/ JSON」, \t成功:功能(數據){ \t \t警報( 「成功:」 +數據); \t}, \t錯誤:功能(XMLHttpRequest參數url errorThrown){ \t \t警報( 「錯誤: 」+ textStatus +「/」 + errorThrown); \t} });' 我仍然有「成功:未定義」。 –

回答

0

請確保標題正確,爲json響應,如raina77ow建議。

對JSON正確的標題是「應用/ JSON的」,這頭象這樣設置:

header('Content-type: application/json');