【问题标题】:Angular 2 Cross Doamin Call in local servers is not working本地服务器中的 Angular 2 跨域调用不起作用
【发布时间】:2017-08-25 20:45:37
【问题描述】:

我的 Angular 2 应用程序在 8100 端口上运行,而 API 项目是使用节点构建的,并且在不同的端口 3003 上运行。API 项目不是休息 API 项目。

当我尝试从 Rest Client 或 Postman 调用 API 之一时,我收到了响应。

即使尝试从节点调用 API,它也可以正常工作。

从 Angular 2 项目进行 API 调用时,它显示 404 错误。

Angular 2 代码 -

getApplications() {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('cache-control', 'no-cache');
        headers.append('Access-Control-Allow-Origin', '*');

        return this.http.post('http://localhost:3003/applications/list', {}, {})
            .map(res => res.json());
}

NodeJS 工作请求 -

var request = require("request");

var options = { 
  method: 'POST',
  url: 'http://localhost:3003/application/list',
  headers: 
   {
     'cache-control': 'no-cache',
     'content-type': 'application/json' 
   } 
 };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

邮递员请求 -

【问题讨论】:

    标签: angular localhost cross-domain


    【解决方案1】:

    我猜你打错了:

    浏览器: http://localhost:3003/application/list

    在 Angular 中: http://localhost:3003/applications/list

    【讨论】:

    • 非常感谢!我不相信我是怎么错过的。
    猜你喜欢
    • 2020-01-30
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2018-04-12
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多