API http调用在Angular 5中使用httpClient返回401 Unauthorized

时间:2018-04-27 13:37:49

标签: angular api authorization httpclient unauthorized

return this.http.get<any>('https://test.p-44.com/api/v3/tl/shipments/'+id+'/statuses?includeMapUrl=true', {
      headers:
        new HttpHeaders()
          .append('Accept', 'application/json')
          .append('Content-Type', 'application/json')
          .append('Authorization', 'Basic ' + btoa("xxxxx:xxx"))
      , responseType: 'json'
    })
    .map((response: Response) => <any>response)

这是我对Api的服务调用,这将返回错误:

无法加载https://test.p-44.com/api/v3/tl/shipments/1252/statuses?includeMapUrl=true:对预检请求的响应未通过访问控制检查:否&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; https://dev.18th-avenue.com&#39;因此不允许访问。响应的HTTP状态代码为401。

但是当我使用代理配置时,此调用在我的locahost上完全正常:

const PROXY_CONFIG = [
  {
      context: [
          "/44",
      ],
      target: "https://test.p-44.com",
      secure: false,
      changeOrigin: true,
      pathRewrite: {"^/44" : ""}

  }
]
module.exports = PROXY_CONFIG;


return this.http.get<any>('/44/api/v3/tl/shipments/'+id+'/statuses?includeMapUrl=true', {
      headers:
        new HttpHeaders()
          .append('Accept', 'application/json')
          .append('Content-Type', 'application/json')
          .append('Authorization', 'Basic ' + btoa("pmorin@18th-avenue.com:Action18!"))
      , responseType: 'json'
    })
    .map((response: Response) => <any>response)

你知道为什么这个代理呼叫有效但没有代理呼叫没有?我使用IIS服务器,我需要在那里添加配置吗?当我在我的服务器上构建我的应用程序时,我无法使用该代理

谢谢

0 个答案:

没有答案