【发布时间】:2015-07-01 10:10:18
【问题描述】:
当我使用 Fiddler 或任何浏览器端 HTTP 客户端扩展(如 Advanced Rest Client)时,我可以轻松地从 API 获取数据。 但是当我尝试使用来自 Angular JS 的相同 API 时,CORS 问题会立即出现。我已经尝试使用以下几行来解决 AngularJS 中的问题,但不幸的是它没有用。
appd.config(function ($httpProvider) {
// $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// $httpProvider.defaults.headers.post['Accept'] = '*/*'
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
delete $httpProvider.defaults.headers.common["X-Requested-With"];
$httpProvider.defaults.headers.common["Accept"] = "*/*";
$httpProvider.defaults.headers.common["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.headers.common['Authorization'] = 'Basic encodedpassword=';
});
我在 mozilla 中遇到以下错误:
跨域请求被阻止:
The Same Origin Policy disallows reading the remote resource at http://host/training_webapi. This can be fixed by moving the resource to the same domain or enabling CORS.
并在 Chrome 中关注:
XMLHttpRequest cannot load http://host/training_webapi The request was redirected to 'http:host/training_webapi/', which is disallowed for cross-origin requests that require preflight.
【问题讨论】:
标签: javascript angularjs cross-domain cross-platform cors