简体   繁体   中英

AngularJS 'No 'Access-Control-Allow-Origin' header is present on the requested resource' error

I am using AngularJS 1.2.20.

Whenever I use the $http service to access a zip file from a URL (different domain/server), it gives me an error as following:

XMLHttpRequest cannot load http://localhost/ABC/updates.zip. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

I've tried setting the following in the config of my module:

$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
delete $httpProvider.defaults.headers.common["X-Requested-With"];
$httpProvider.defaults.headers.common["Accept"] = "application/json";
$httpProvider.defaults.headers.common["Content-Type"] = "application/json";

But this still throws the error.

My code snippet is :

$http({method: 'GET', url: paths.categoryUpdateURL}).
success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    alert("download Complete")
}).
error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    alert("error")
});

What should be done to prevent this?

除非服务器提供JSONP API或允许配置Access-Control-Allow-Origin响应属性,否则除非请求服务器请求ZIP文件,否则您将无能为力,因为浏览器不允许这样做。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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