2的WebAPI - CORS与应用的contentType / JSON工作 [英] WebAPI 2 - CORS not working with contentType application/json

查看:249
本文介绍了2的WebAPI - CORS与应用的contentType / 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 controller

If 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 my BookingController 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆