我在* .svc文件中使用WebSriptServiceHostFactory设置了一个ASP.NET Ajax服务 - 没有web.config配置。在合同中,我从两个非常简单的方法开始:
[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 Method Not Allowed”可能因为合同使用[两个方法的WebGet]属性
所以现在405消息消失了,我继续直接在我的浏览器中调用这两种方法,然后返回预期的结果。但是,当使用我在上面设置的jQuery代码调用这两个方法时,不会返回任何内容。关于我做错了什么的想法?