WCF REST服务返回405:方法不被允许的jQuery AJAX
问题描述:
即使要通过这个帖子 wcf REST Services and JQuery Ajax Post: Method not allowed (3)WCF REST服务返回405:方法不被允许的jQuery AJAX
我不能让过去405后GET:试图调用一个方法经由我的WCF REST服务时不得法下面的AJAX调用:
$.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
}
上运行成线后跨域问题,我手动添加一个Global.asax文件的Web服务项目,还增加了以下方法方法:
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:方法不允许。
TIA。
答
我有完全相同的问题,除了我正在做纯html项目,因此没有地方追加头,因为我没有c#。
我有人有这个答案,请张贴它......它一直让我疯狂!
感谢
答
我在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();
}
}
由于解决了这个问题,你解决了我的问题,这些线“HttpContext.Current.Response.AddHeader(” Access-Control-Allow-Methods“, ”GET,POST“);” – 2013-04-01 12:35:45