2013-06-19 62 views
0

我是相當新的JS,我已經嘗試通過Ajax調用發送GET請求到我的服務器時遇到的問題。服務器正在接收請求並正在執行get,並且ajax調用成功(執行成功函數),但響應爲空。在郵差測試中,GET工作沒有問題。Ajax的GET請求爲空當成功

AJAX調用:

url = "http://localhost:8080/EmployeeLookup/SearchServlet?search=test" 
$.ajax({ 
    url: url, 
    type: 'GET', 
    success: display, 
    error: function(xhr,status,error){ alert(status); }, 
}); 

的請求被髮送到這個頁面投放在同一臺服務器,所以不存在與訪問控制允許來源的任何問題。

任何幫助,將不勝感激。

在此先感謝。

編輯:

顯示功能:只是測試它是否現在爲空。用===進行測試也會返回true。

function display(json) { 
    if(json == null) { 
     $("#text").replaceWith(json); 
    } else { 
     $("#text").replaceWith("Response was null. "); 
    } 
} 

發送回看起來像JSON:

{ 
    "seachResult": "someresult" 
} 
+1

向我們展示了您如何處理回覆。 –

+1

什麼是「顯示」? –

+0

這是一個跨源請求嗎?獲取請求不需要提前請求,但仍需要訪問控制 - 允許來源 – Musa

回答

2

你應該總是指定的dataType讓jQuery將始終如一地解析正確。

// the http portion is not needed for same-domain requests 
var url = "/EmployeeLookup/SearchServlet?search=test" 
$.ajax({ 
    url: url, 
    type: 'GET', 
    dataType: 'json', 
    success: display, 
    error: function(xhr,status,error){ alert(status); }, 
}); 

function display(json) { 
    console.log(json); 
} 

測試,如果返回的數據是要採取比測試它是否爲空較多,它永遠不會,如果你使用jQuery 1.9+和JSON的dataType爲空。

+0

要建議測試沒有數據的正確方法,我們需要查看結果並且沒有結果的數據示例。 –

+0

我試着將dataType設置爲json,並且響應仍然爲空。 ajax是否需要一些特殊的頭部來讓它接受一個響應體? –

+0

不,你的回答是什麼樣的? –