简体   繁体   English

Express JS中的CORS问题

[英]CORS issue in express JS

I am getting cors error with this code. 我收到此代码的错误提示。 I have applied the cross origin headers too. 我也已经应用了交叉原点头。 Please tell me the solution for this. 请告诉我解决方案。

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var router = express.Router();
app.use(bodyParser.json());

app.all('/*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
 });


router.get('/',function(req,res){
    res.send('Yippeee! You found me.');
});
router.post('/postTest',function(req,res){
    console.log(req.body);
    res.json(req.body);
});

app.use('/',router);

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

It's hard to give a specific answer because there's so many unknowns in your question. 给出具体答案很困难,因为您的问题有很多未知数。 However you could give the CORS plugin a shot: 但是,您可以尝试一下CORS插件

var cors = require('cors');

app.use(cors());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS 标头问题 express Angular - CORS headers issue express Angular Angular 5节点js表示POST JSON Obj。 到Amazon Aws [外部API](以前称为CORS问题) - Angular 5 node js express POST JSON Obj. to Amazon Aws [External API] (formerly CORS issue) 在不同的端口上运行时,Cors在rest api应用服务器(express)和Angulars js应用程序之间发出问题 - Cors issue between rest api application server(express) and Angulars js application while running on different ports 当休息api应用服务器(express)和Angulars js应用程序在不同端口上运行时,Cors会发出问题 - Cors issue when rest api application server(express) and Angulars js application running on different port Cors 托管在 Vercel 上的 React 应用程序与托管在 Azure 上的容器应用程序中的 Express JS API 之间的问题 - Cors issue between React app hosted on Vercel and an Express JS API in a container app hosted on Azure Next.js 和 Express 的 CORS 问题:对预检请求的响应未通过访问控制检查: - CORS issue with Next.js and Express: Response to preflight request doesn't pass access control check: 快速路由器端点的 CORS 问题 - CORS issue with Express Router end points 表达app.get('/ *')和api CORs问题 - express app.get(' /* ') and api CORs issue 在本地使用 NodeJs、Express 和 React 的 Cors 问题 - Cors issue using NodeJs, Express and React locally Vue.js 的 CORS 问题 - CORS issue with Vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM