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

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

我正在尝试 post 一个简单的 JSON 对象到我的本地 asp.net web.api 服务。

这里是代码的和平。

    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 启用了 Cors,这种代码的和平适用于 Internet Explorer。但是 Google 的 Chrome 留下错误:OPTIONS: http://localhost:50827/api/v0.1/xxxx/create-some-table 404 (Not Found)

同一个 webapi 控制器有一些其他的 GET 方法,而且效果很好。

有什么想法吗?

感谢任何帮助。

帕夫洛

所以这似乎是一个已知问题,其中 Chrome 在发送实际的跨源请求之前使用 OPTIONS 动词发送 "pre-flight" 请求。

有些情况下它不会发送飞行前请求,但不幸的是发送 JSON 不是其中一种情况。

您似乎没有设置 ASP.NET WebAPI 服务器来处理这些类型的飞行前检查请求。可能是因为启用对它的支持显然不是默认设置。

幸运的是,网络上有很多帮助可以帮助您解决此问题。这里有两个链接可以帮助您入门。

相关的 Whosebug 问题: AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?

这似乎是 how to configure CORS properly if you are using ASP.NET WebAPI 2 上的有用页面。

哦,我假设如果飞行前请求失败,Chrome 将不会执行正确的 CORS 请求。在我的快速研究中,我没有看到任何地方可以证实或否认这一点,但这对我来说是合乎逻辑的,因为飞行前请求的全部意义在于找出服务器支持哪种 CORS 请求,或者如果完全支持的话。