使用Angular时,不存在“Access-Control-Allow-Origin”标头

时间:2017-12-01 21:09:07

标签: node.js angular express cors

我正在使用 angular - >尝试oauth到twitter 节点 - > twitter ,但在向混音添加角度时,我一直收到错误No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access。我的应用配置..

  • 在端口4200上运行Angular UI
  • 在端口3000上运行的Express Node Server
  • 服务器上的CORS设置为Allow-Access-Origin localhost:4200 for Angular to Node api来电。

使用调用呼叫快速路由的快速模板可以正常工作,如下所示

模板 - >本地主机:3000 / AUTH /微博

Response Headers
    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:http://localhost:4200
    Location:https://api.twitter.com/oauth/authenticate?oauth_token=placeholdertokenstackoverflow
    Vary:Origin

Request Headers
    Host:localhost:3000
    Referer:http://localhost:3000/

localhost:3000 / auth / twitter - > //api.twitter.com/oauth /...

GENERAL
    Request URL:https://api.twitter.com/oauth/authenticate?oauth_token=placeholdertokenstackoverflow
    Request Method:GET
    Status Code:200 

Request Headers
    :authority:api.twitter.com
    :method:GET
    :scheme:https
    referer:http://localhost:3000/

当使用angular来调用express路由而不是模板时,我认为是一个CORS错误,如下所示

angular.http.get - >本地主机:3000 / AUTH /微博

RESPONSE HEADERS
    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:http://localhost:4200
    Location:https://api.twitter.com/oauth/authenticate?oauth_token=placeholdertokenstackoverflow
    Vary:Origin

REQUEST HEADERS
    Host:localhost:3000
    Origin:http://localhost:4200
    Referer:http://localhost:4200/

localhost:3000 / auth / twitter - > //api.twitter.com/oauth /...

GENERAL
    Request URL:https://api.twitter.com/oauth/authenticate?oauth_token=placeholdertokenstackoverflow
    Request Method:GET
    Status Code:200 

REQUEST HEADERS
    :authority:api.twitter.com
    :method:GET
    :scheme:https
    ***origin:null
    referer:http://localhost:4200/***

当角度调用快速路线时,会出现上面的origin:null。即使Express服务器在两个实例中都设置相同并调用twitter,但标题是不同的,我不明白。角度设置视图而不是表达会使浏览器对请求的来源感到困惑吗?

1 个答案:

答案 0 :(得分:1)

修改节点服务器以添加标头Access-Control-Allow-Origin:*以从任何地方启用跨源请求(或指定域而不是*)。这应该可以解决您的问题。 以下是我解决问题的方法:

<div><p style="color: red" data-bind="validationMessage: personal().postal"></p></div>
相关问题