我正在使用MEAN Stack編寫Web應用程序,並且遇到無法解決的問題,無論我嘗試或搜索什麼。請求的資源上沒有「Access-Control-Allow-Origin」標頭
我想用ExpressJS登錄使用護照/護照-Facebook。如果我在我的瀏覽器的URL中寫入localhost/api/auth/facebook
,一切運行良好。
但是,如果我在我的HTML代碼中創建了一個像這樣的代碼<a href="/api/auth/facebook">Login with Facebook</a>
,它會將我帶到我的404 page
(請參見下面的nginx)。 如果我嘗試使用角度方式,例如<button ng-click="login_fb()">Login with Facebook</button>
,則會出現No 'Access-Control-Allow-Origin' header is present on the requested resource.
錯誤。
我相信,我應該使用<a href="...">
元素,但我覺得我的nginx的腳本阻止它,因爲我啓用了CORS我的服務器的NodeJS上,與此:
server.all('/*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
next();
});
我會離開我的nginx的劇本太(忽略內部的「<」塊和「>」):
worker_processes 4;
events {
worker_connections 1024;
}
http {
upstream backend {
server 127.0.0.1:8080;
}
# To optimize the response of static files
sendfile on;
server {
# Listen to normal port
listen 80;
# Let through files like CSS, JPG, JS, ...
include mime.types;
# Alias for this server
server_name <my-domain>;
# Where the HTML files are located
root <my-folder>;
# Route for API calls
location /api/ {
proxy_pass http://backend;
}
# Route for the index
location ~ ^/*$ {
index index.html index.htm;
}
# Route for 404 page
error_page 404 /index.html;
}
}
編輯:使用這樣<a href="http://localhost:8080/api/auth/facebook">Login with Facebook</a>
鏈接,但它迫使我使用的URL硬編碼的,我將最終把這個應用程序域下。
新的編輯:我知道,像這樣使用<a href="/api/auth/facebook">Login with Facebook</a>
不起作用,因爲它認爲它一個角路線,因爲我還沒有在$ routeProvider宣佈,它帶我到404