gpt4 book ai didi

javascript - Angular 5 Node js 表达 POST JSON Obj。到 Amazon Aws [外部 API](以前的 CORS 问题)

转载 作者:行者123 更新时间:2023-11-30 14:54:50 24 4
gpt4 key购买 nike

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

我的服务器运行在 localhost:3000

我在 localhost:4200 上开发

我正在创建一些东西并尝试将其发布到 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、不使用 cors 并附加 res.headers 以及我能想到的所有其他变体来发布到该 API

我仍然遇到这个错误,这个错误在这里很常见,但他们的解决方案似乎对我不起作用。仍然收到此错误...

Failed to load http://Amazon-API:port/send: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.

那是来自 NETWORK 标签:

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

非常感谢任何形式的帮助

最佳答案

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

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();
});

关于javascript - Angular 5 Node js 表达 POST JSON Obj。到 Amazon Aws [外部 API](以前的 CORS 问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47461859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com