簡體   English   中英

角度:“ Access-Control-Allow-Origin”標頭出現在請求的資源上

[英]Angular: 'Access-Control-Allow-Origin' header is present on the requested resource

我讀過很多相同或相似的問題,幾乎所有的答案都是配置服務器。 我的客戶端應用程序是Angular.js "ionic"我的服務器應用程序是node.js並部署在Heroku 這是我的服務器配置:

// set up CORS resource sharing
app.use(function(req, res, next){
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.header('Access-Control-Allow-Credentials', true);
    next();
})

注意:當我在本地運行服務器時,我的客戶端通常與服務器通信

我的客戶端應用程序無法發出任何http請求,並出現此錯誤:

XMLHttpRequest cannot load https://myheroku.herokuapp.com/api/x. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 503.

這是請求標頭:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ar;q=0.6,fi;q=0.4,it;q=0.2,en-GB;q=0.2,en-CA;q=0.2
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:myheroku.herokuapp.com
Origin:http://localhost:8100
Referer:http://localhost:8100/

很抱歉,如果這是一個愚蠢的錯誤,但對於像我這樣的初學者來說可能會很有用。 我檢查了我的服務器應用程序日志heroku logs並發現了這個

heroku process failed to bind to $PORT within 60 seconds of launch

並且由於heroku將端口分配給應用程序,因此我如何解決此問題:來自:

var server = app.listen(3000, function(){
    console.log('App listeining on', server.address().port);
})

至:

var server = app.listen(process.env.PORT || 5000, function(){
    console.log('App listeining on', server.address().port);
})

暫無
暫無

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

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