我有一个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