簡體   English   中英

當休息api應用服務器(express)和Angulars js應用程序在不同端口上運行時,Cors會發出問題

[英]Cors issue when rest api application server(express) and Angulars js application running on different port

我有一個用node.js編寫的休息api應用程序&express運行在端口3000和在同一服務器上的端口9001上運行的angularjs應用程序。 在從angularjs應用程序調用rst api時,它會給出cors問題。

在休息api應用程序中,我使用了“cors”模塊

var cors = require('cors');

app.use(cors());

但它給了我以下錯誤

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/user/login. This can be fixed by moving the resource to the same domain or enabling CORS

請求標頭和響應標頭如下

響應頭

Content-Length 222
Content-Type application/json; charset=utf-8
Date Tue, 16 Dec 2014 08:40:05 GMT
X-Powered-By Express

查看源

請求標題

Accept application/json, text/plain, /
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Authorization null
Content-Length 47
Content-Type application/json;charset=utf-8
Host localhost:3000
Origin http://localhost:9001
Referer http://localhost:9001/
User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0

謝謝

我通過添加添加行app.options('*',cors())來解決它; 就在所有路線之前。

這是一個帶有http-request-header“OPTIONS”的預檢請求問題

您可以將解決方案設置為

app.configure('development', function(){
    app.set('origins', '*:*');
}

暫無
暫無

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

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