2的WebAPI - CORS与应用的contentType / JSON工作 [英] WebAPI 2 - CORS not working with contentType application/json
问题描述
我有一个Asp.net的WebAPI 2项目,以预订
控制器
如果我试图从我的客户端JS发布此:
$。阿贾克斯({
网址:的http://本地主机:57517 / API /预约,
输入:POST,
跨域:真实,
数据:{AdultPaxCount:1},
的contentType:应用/ JSON
});
我得到一个不容许误差(和我的 BookingController
未命中发表
动作) p>
不过;
如果我删除
的contentType:应用/ JSON
块引用>部分,它的工作原理(它被发送到控制器)
我缺少的东西在我的设置?
解决方案您可以添加选项的方法来你的控制器,它总是只返回没有错误,或添加
[HttpOptions]
的[AcceptVerbs(POST,选项)]属性控制器的PostXXX方法。
I've got an Asp.net WebApi 2 project, with a
Booking
controllerIf I try to post this from my client-side js:
$.ajax({ url: 'http://localhost:57517/api/booking', type: 'POST', crossDomain:true, data: {AdultPaxCount:1}, contentType: "application/json" });
I get a not-allowed error (and the
Post
action on myBookingController
is not hit)However;
If I remove the
contentType: "application/json"
section, it 'works' (it is sent to the controller)
Am I missing something in my setup?
解决方案You can either add an Options method to your controller which always just returns without error, or add the
[HttpOptions] [AcceptVerbs("POST", "OPTIONS")]
attributes to the PostXXX Method of your controller.
这篇关于2的WebAPI - CORS与应用的contentType / JSON工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!