Angular 5节点js表示POST JSON对象。 到亚马逊Aws (以前的CORS问题)

已解决,请参阅下面的答案

我的服务器在localhost:3000上运行

我开发localhost:4200

我正在创build一些东西,并试图将其发布到Amazon API上

angular度代码:

 sendSomething(something) { const body = JSON.stringify(something); // const headers = new Headers({'Content-Type': 'application/json'}); const headers = new Headers({'Access-Control-Allow-Origin': '*'}); return this.http.post('http://Amazon-API:port/send', body, {headers: headers}) .map((response: Response) => response.json()) .catch((error: Response) => { this.error.handleError(error.json()); return Observable.throw(error.json()); }); } 

服务器端:

 //////////////app.js////////////// app.use(cors()); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", '*'); //<-- you can change this with a specific url like http://localhost:4200 // res.header("Access-Control-Allow-Credentials", true); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'); next(); }); app.use('http://Amazon-API:port', engineRoutes); //////////////routes/engine.js////////////// router.post('/send', engine_controller.send_something); //////////////controllers/engine.controller.js////////////// exports.send_something = function (req, res, next) { const somethingID = req.body.something; Something.findById(somethingID, function(err, something) { if (err) { res.status(404).json({ title: 'Something not found', error: {message: 'Something went wrong'} }); } console.log(something); if (something) { res.status(200).json({ message: 'Something successfully sent', something: something }); } }) }; 

我曾试着用cors来发布这个API,没有cors和附加的res.headers,以及其他所有我能想到的变体

我仍然得到这个在这里很常见的错误,但是,他们的解决scheme似乎并不适合我。 仍然得到这个错误…

无法加载http:// Amazon-API:port / send :对预检请求的响应未通过访问控制检查:在请求的资源上没有“Access-Control-Allow-Origin”标头。 原因' http:// localhost:4200 '因此不被允许访问。 该响应具有HTTP状态码403。

这是从networking标签:

 Accept:*/* Accept-Encoding:gzip, deflate Accept-Language:he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7 Access-Control-Request-Headers:access-control-allow-origin Access-Control-Request-Method:POST Connection:keep-alive Host:Amazon-API:port Origin:http://localhost:4200 

任何forms的帮助将是如此赞赏

我看到你添加了这个代码,但是我还不能发表评论,你可以尝试在其他路线之前添加这个代码

 app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS'); next(); }); 

解决了,

我所做的是路由回到前面的服务器:

 sendSomething(something) { const body = JSON.stringify(something); const headers = new Headers({'Content-Type': 'application/json'}); return this.http.post('http://localhost:3000/api/somethings/send-something', body, {headers: headers}) .map((response: Response) => response.json()) .catch((error: Response) => { this.error.handleError(error.json()); return Observable.throw(error.json()); }); } 

然后接受这条路线,因为它在后面:

 //////////////app.js////////////////// app.use('/api/somethings/send-something', engineRoutes); /////////////routes/engine.js///////// router.post('/', engine_controller.send_something); 

最重要的是,在控制器本身,我使用新下载的请求库发送我的JSON数据到我的外部API:

 ////////////controlllers/engine.controller.js//////////// const request = require('request'); exports.send_something = function (req, res, next) { const SomethingID = req.body.something; Something.findById(SomethingID, function(err, something) { if (err) { res.status(404).json({ title: 'Something not found', error: {message: 'Something went wrong'} }); } request({ url: app.get('amazon-API:port/method'), method: "POST", json: something }, function (error, response, body) { // console.log(error) <--- returns null or error // console.log(response.statusCode <--- returns 200 / 403 / we // console.log(body) <--- returns response pure html res.status(200).json({ message: 'Something successfully sent', response: body, status: response.statusCode }); }); }) }; 

现在作为回应,我得到了我发布的服务器发回给我的信息,这正是我所需要的。

最终我想到了我的方式感谢其他许多问题张贴在这里

所以再次感谢SOF!