Node.js 向-NodeJS/heroku中的外部API发送get请求时CORS出现问题
我有一个内置“Angular/NodeJs”的应用程序,它使用heroku服务器。 在应用程序中,我可以选择在“omdbapi”中搜索电影。在我执行搜索之前,一切都很正常,控制台会给我下一个错误: 当我在chrome中打开CORS扩展时,它工作得很好 有人能告诉我有什么问题吗 节点代码Node.js 向-NodeJS/heroku中的外部API发送get请求时CORS出现问题,node.js,heroku,Node.js,Heroku,我有一个内置“Angular/NodeJs”的应用程序,它使用heroku服务器。 在应用程序中,我可以选择在“omdbapi”中搜索电影。在我执行搜索之前,一切都很正常,控制台会给我下一个错误: 当我在chrome中打开CORS扩展时,它工作得很好 有人能告诉我有什么问题吗 节点代码 const express = require('express'); const bodyParser = require('body-parser'); const mongoose
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
const path = require('path');
const compression = require('compression');
const port = process.env.PORT || 8000;
const cors = require('cors')
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
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(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/images", express.static(path.join(__dirname, "images")));
app.use("/", express.static(path.join(__dirname, "angular")));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, "angular", "index.html"));
});
app.listen(port, () => {
console.log("App is running on port " + port);
[编辑]
将标题更改为@jbarros后,dot仍在工作
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
const path = require('path');
const compression = require('compression');
const port = process.env.PORT || 8000;
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", 'Authorization, Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
next();
});
角get函数
getMoviesByName(movieName: string) {
const url = 'https://www.omdbapi.com/?s=' + movieName + '&apikey=xxxxxxx&type=movie'
return this.http.get<{Search: ImportedMovieModal[]}>(url)
}
getMoviesByName(movieName:string){
常量url=https://www.omdbapi.com/?s=“+movieName+”&apikey=xxxxxxx&type=movie”
返回此.http.get(url)
}
您不允许在访问控制允许标头中使用授权
标头。
因此,添加标题:
更改:
res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
用于:
此外,您可能需要截获OPTIONS方法以发送HTTP ok状态,因此:
更改您的:
next()
用于:
并将其重新部署到Heroku。您必须指定要在访问控制允许标头字段中使用的标头。可能您没有提到Access Control Allow标头中的某些标头。此外,您不需要在Access Control Allow标头中指定内容类型和Accept。他们总是被允许@jbaross谢谢你的帮助。我编辑了我的问题。我不明白是什么问题there@Igorromanovsky编辑后是否会出现相同的错误?别忘了重新启动服务器:-)@jbaross编辑后我将直接部署到heroku@jbaross感谢您的帮助,但它仍然不起作用,可能是因为get请求是从angular而不是从node发送的?(添加了角度请求功能)在重新部署到Heroku后,您是否可以在网络选项卡中发布Chrome开发工具显示的任何进一步信息?
next()
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}