簡體   English   中英

AngularJS'所請求的資源上沒有'Access-Control-Allow-Origin'標頭'錯誤

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

我正在使用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")
});

應該采取什么措施來防止這種情況?

除非服務器提供JSONP API或允許配置Access-Control-Allow-Origin響應屬性,否則除非請求服務器請求ZIP文件,否則您將無能為力,因為瀏覽器不允許這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM