【发布时间】:2015-05-01 08:32:53
【问题描述】:
我正在对 WCF REST WebServices 进行一些测试,但我遇到了 POST 调用。 我创建了一个 web 服务,它公开了一些关于良好的 ol' Northwind DB 的测试数据,因为我希望从测试 HTML 页面在本地使用它,并且因为我想测试 CORS 功能,所以我通过遵循这些使其符合 CORS指令http://enable-cors.org/server_wcf.html。
不幸的是,当我进行 POST 调用时出现问题。 不像 GET 调用(效果很好),POST 调用会抛出这个错误:
这是什么鬼?似乎“Access-Control-Allow-Origin”标头未在客户端正确管理,因为在我的 EnableCrossOriginResourceSharingBehavior WCF 类中,方法“ApplyDispatchBehavior”(它过滤了到达请求的“Access-Control-Allow-Origin”标头) 在我进行 POST 调用时被命中,但随后 Ajax 调用失败。
这是我的 jQuery Ajax 发布命令:
//Create new object
var item = {
"CustomerId": "0",
"CompanyName": "prova"
};
//Push object
$.ajax({
type: "POST",
url: 'http://localhost:3434/NorthwindService.svc/Customer/Create',
crossDomain: true,
headers: {'Access-Control-Allow-Origin' : '*'},
data: JSON.stringify(item),
success: function (data) {
alert('ok!');
},
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
This 是我的 WCF 服务 Visual Studio 2013 项目。 要对其进行测试,您只需将 web.config 中的“NorthwindConnectionString”设置为现有的。我遇到问题的网络服务方法是“http://localhost:3434/NorthwindService.svc/Customer/Create”方法的 POST,所有其他方法都可以正常工作。 这是我的方法契约的预览:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "Customer/Create", BodyStyle=WebMessageBodyStyle.WrappedRequest)]
void NewCustomer(CustomerDTO customer);
提前致谢。
【问题讨论】:
标签: ajax wcf rest post http-status-code-405