简体   繁体   English

AngularJS $ http.post不适用于Chrome,仅适用于IE

[英]AngularJS $http.post doesn't work in Chrome, only IE

I am trying to post a simple JSON object to my local asp.net web.api service. 我正在尝试将一个简单的JSON对象发布到我的本地asp.net web.api服务中。

here is a peace of code. 这是和平的代码。

    createTable: function (table) {
        var res = $http.post('http://localhost:50827/api/v0.1/xxxx/create-some-table', table);
        res.success(function (data, status, headers, config) {
            alert("Sent : " + JSON.stringify({ data: data }));
        });
        res.error(function (data, status, headers, config) {
            alert("failure message: " + JSON.stringify({ data: data }));
        });
    }

Asp.Net Web Api has Cors enabled and this peace of code works well with internet explorer. Asp.Net Web Api启用了Cors,这种代码的安全性非常适合Internet Explorer。 But Google's Chrome leaves error: OPTIONS: http://localhost:50827/api/v0.1/xxxx/create-some-table 404 (Not Found) 但是Google的Chrome留下了错误:选项: http:// localhost:50827 / api / v0.1 / xxxx / create-some-table 404(未找到)

Same webapi controller has some other GET methods and it works well. 相同的webapi控制器还有其他一些GET方法,并且效果很好。

Any idea? 任何想法?

Appreciate any help. 感谢任何帮助。

Pavlo 帕夫洛

So it seems like this is a known issue where Chrome sends a "pre-flight" request using the OPTIONS verb before sending the actual cross origin request. 因此,这似乎是一个已知问题,其中Chrome在发送实际的跨源请求之前先使用OPTIONS动词发送“飞行前”请求。

There are cases when it will not send a pre-flight request, but unfortunately sending JSON is not one of those cases. 在某些情况下,它不会发送预检请求,但不幸的是,发送JSON并非其中一种情况。

It seems like you don't have the ASP.NET WebAPI server set up to handle these types of pre-flight check requests. 似乎您没有设置ASP.NET WebAPI服务器来处理这些类型的飞行前检查请求。 Probably because it is apparently not the default to enable support for it. 可能是因为它显然不是启用对此功能的默认支持。

Luckily, there is plenty of help out there on the web to help you fix this. 幸运的是,网络上有很多帮助您解决此问题的帮助。 Here's two links to get you started. 这里有两个链接可帮助您入门。

Relevant StackOverflow question: AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE? 相关的StackOverflow问题: Chrome中的AJAX发送选项而不是GET / POST / PUT / DELETE吗?

This seems like a helpful page on how to configure CORS properly if you are using ASP.NET WebAPI 2 . 如果您使用的是ASP.NET WebAPI 2,这似乎对如何正确配置CORS很有帮助

Oh, and I am assuming that if the pre-flight request fails, Chrome will not do the proper CORS request. 哦,我假设如果飞行前请求失败,Chrome将不会执行正确的CORS请求。 I didn't see anywhere in my quick research to confirm or deny that, but it makes logical sense to me, since the whole point of the pre-flight request is to find out what sort of CORS requests are supported by the server, or if it is supported at all. 我没有在快速研究中看到任何地方可以确认或否认这一点,但是对我来说,这是合乎逻辑的,因为飞行前请求的全部目的是找出服务器支持哪种类型的CORS请求,或者如果完全受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM