調用WCF RESTful服務以下是我的代碼jQuery腳本:無法從jQuery的
$(document).ready(function() {
$.ajax({
cache: false,
type: "GET",
async: true,
dataType: "json",
url: "http://localhost:9002/SampleServices/Service/REST/Employees",
processdata: false,
error: ServiceFailed,
success: function() { alert("succeeded"); }
});
});
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
Url = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function ServiceFailed(xhr) {
alert("error");
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
當我運行上面的代碼總是進入ServiceFailed(XHR)和警報「錯誤」。
當我嘗試通過瀏覽器執行上述url(「http:// localhost:9002/SampleServices/Service/REST/Employees」)時,我得到以下JSON響應: {「page」:1, 「:1,」 行 「:[{」 小區 「:[」 1" , 「哈里斯」, 「21-03-1979」, 「HR」], 「ID」:1}], 「總」:1}
菲德勒說以下,當我運行jQuery腳本:
# Result Protocol Host URL Body Caching Content-Type Process Comments Custom
1 200 HTTP localhost:9002 /SampleServices/Service/REST/Employees?_=1340833203960 89 application/json; charset=utf-8 chrome:1448
是請求從比服務不同的域來的跨域支持?如果是這樣,那麼您遇到了跨域請求問題。 – carlosfigueira
你有兩個'ServiceFailed'的定義。那是不對的。 Javascript是動態類型的,因此運行時無法區分這些定義。這可能是因爲你正在想象,Javascript會根據你提供的輸入參數的類型和數量調用一個不同的'ServiceFailed',就像在C#或Java中一樣。這不是在Javascript中發生的事情。另外:通常,Javascript函數會獲得小寫字母或駝峯名稱。 – Cheeso