0

我正在使用 AngularJS 1.2.20。

每当我使用该$http服务从 URL(不同的域/服务器)访问 zip 文件时,它都会给我一个错误,如下所示:

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.

我尝试在模块的配置中设置以下内容:

$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";

但这仍然会引发错误。

我的代码片段是:

$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")
});

应该做些什么来防止这种情况发生?

4

1 回答 1

0

除非服务器提供JSONP API,或者允许配置 Access-Control-Allow-Origin 响应属性,否则除了请求服务器请求 ZIP 文件之外,您不能做任何事情,因为浏览器不允许这样做。

于 2014-08-24T21:25:25.913 回答