WCF REST 服务返回 405:jQuery AJAX GET 不允许的方法
Posted
技术标签:
【中文标题】WCF REST 服务返回 405:jQuery AJAX GET 不允许的方法【英文标题】:WCF REST service returns 405: method not allowed for jQuery AJAX GET 【发布时间】:2012-04-21 12:53:45 【问题描述】:即使看完这篇文章 wcf REST Services and JQuery Ajax Post: Method not allowed (3)
我无法通过 405:尝试通过以下 AJAX 调用在我的 WCF REST 服务上调用方法时不允许使用方法:
$.ajax(
type: "GET",
url: 'http://mydomain.com/service1/service.svc/json/getsupportedagencies',
contentType: "application/json; charset=utf-8",
dataType: "json",
failure: function (msg)
alert(msg);
,
success: function (agencies)
alert("success via REST");
$.each(agencies, function (i, a)
viewModel.agencies.push(new agency(a.Name, a.FullName));
);
);
服务接口定义如下:
[ServiceContract]
public interface IService1
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, UriTemplate = "getsupportedagencies")]
AgencyDTO[] GetSupportedAgencies();
// other methods
在跨域问题上遇到线程后,我手动在webservice项目中添加了一个Global.asax文件,同时添加了如下方法方法:
protected void Application_BeginRequest(object sender, EventArgs e)
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
"http://localhost:80");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
"1728000");
HttpContext.Current.Response.End();
我在 web.config 中的服务模型配置如下:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="ServiceBehavior" name="Services.Service1">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="basic" binding="basicHttpBinding" contract="Services.Contracts.IService1" />
<endpoint address="json"
behaviorConfiguration="JsonBehavior"
binding="webHttpBinding"
contract="Services.Contracts.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
我使用以下代码对服务进行的单元测试通过了:
[Test]
public void TestGetAgencyForLocation()
var client = new WebClient();
var response = client.DownloadString(
"http://mydomain.com/service1/service.svc/json/getsupportedagencies");
Assert.IsTrue(!string.IsNullOrEmpty(response));
var agencies= JsonConvert.DeserializeObject(response);
Assert.IsNotNull(agencies);
如果我在 Chrome 或 IE 中发布 URL“http://mydomain.com/service1/service.svc/json/getsupportedagencies”,我会得到 JSON 格式的正确服务器响应。
是的,所有这些都到位后,我仍然得到 405: method not allowed。
TIA。
【问题讨论】:
谢谢你用“HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");"这一行解决了我的问题 【参考方案1】:我通过在 WCF 项目 Global.asax 文件中添加以下代码解决了这个问题
protected void Application_BeginRequest(object sender, EventArgs e)
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
【讨论】:
以上是关于WCF REST 服务返回 405:jQuery AJAX GET 不允许的方法的主要内容,如果未能解决你的问题,请参考以下文章
无扩展 REST WCF 服务为 PUT 方法返回 http 405
405 Method Not Allowed - 当从 jQuery 发送对象到 rest WCF
Ajax POST 到 WCF Rest CORS 兼容的 WebService 引发错误 405