Angular 2 本地服务器中的跨域调用不工作
Angular 2 Cross Doamin Call in local servers is not working
我的 Angular 2 应用程序是 运行 在 8100 端口和 API 使用节点构建的项目并在不同的端口 3003 表示 运行。 API 项目不是休息 API 项目。
当我尝试从 Rest Client 或 Postman 调用 API 之一时,我得到了响应。
即使尝试从节点调用 API,它也工作正常。
从 Angular 2 项目进行 API 调用时显示 404 错误。
Angular2码-
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);
});
邮递员请求 -
我猜你打错了:
浏览器:
http://localhost:3003/application/list
在Angular中:
http://localhost:3003/applications/列表
我的 Angular 2 应用程序是 运行 在 8100 端口和 API 使用节点构建的项目并在不同的端口 3003 表示 运行。 API 项目不是休息 API 项目。
当我尝试从 Rest Client 或 Postman 调用 API 之一时,我得到了响应。
即使尝试从节点调用 API,它也工作正常。
从 Angular 2 项目进行 API 调用时显示 404 错误。
Angular2码-
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);
});
邮递员请求 -
我猜你打错了:
浏览器: http://localhost:3003/application/list
在Angular中: http://localhost:3003/applications/列表