我有一個ASP.NET * Ajax服務使用* .svc文件中的WebSriptServiceHostFactory設置 - 沒有web.config配置。在合同中,我開始有兩個很簡單的方法:WCF Ajax服務返回空白到jQuery的請求
[OperationContract()]
[WebGet]
string GetPersonalInformationLabel();
[OperationContract()]
[WebGet]
string GetCorporateInformationLabel();
我的jQuery的建立如下:
$.ajaxSetup({
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
dataFilter: function(data){
var msg;
if( typeof(JSON) !== 'undefined' &&
typeof(JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if(msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
}
});
$("#chkCorporateGift").click(function(){
if($(this).is(":checked")){
$.ajax({
type: "GET",
url: "http://localhost/Services/OG.svc/GetCorporateInformationLabel",
success: function(msg){
$("#lblInformationType").text(msg);
}
});
}
else {
$.ajax({
type: "GET",
url: "http://localhost/Services/OG.svc/GetPersonalInformationLabel",
success: function(msg){
$("#lblInformationType").text(msg);
}
});
}
});
正如你所看到的,ajaxSetup分配類型爲「POST 「,但我不得不在下面的兩個調用中用」GET「覆蓋它,因爲我得到了」405方法不允許「,可能是因爲合同在兩個方法上都使用了[WebGet]屬性
因此,現在405消息已經不存在了,我繼續在瀏覽器中直接調用這兩個方法,然後返回expec結果。但是,當使用上面設置的jQuery代碼調用這兩個方法時,沒有返回結果。任何想法,我做錯了什麼?
使用此代碼的頁面是否也來自'localhost'? – 2010-09-30 12:45:43
不,使用jQuery的html文件位於IIS目錄之外的本地文件中,而服務在本地主機上的IIS中運行。只要我將html文件移動到本地主機的虛擬目錄,它就起作用了。但是,我無法更改的值。沒有錯誤返回在控制檯以及...嗯 – alkos333 2010-09-30 13:28:46