【发布时间】:2010-09-30 12:41:57
【问题描述】:
我在 *.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 代码调用这两个方法时,没有返回任何内容。关于我做错了什么有什么想法吗?
【问题讨论】:
-
使用此代码的页面是否也由
localhost提供? -
不,带有 jQuery 的 html 文件位于 IIS 目录之外的本地文件中,而该服务在 IIS 上的 localhost 中运行。一旦我将 html 文件移动到 localhost 的虚拟目录,它就可以工作了。但是,我无法更改 的值。控制台中也没有返回错误......嗯