【发布时间】:2020-02-25 16:24:21
【问题描述】:
在我的入口文件中,我使用了 cors() 并且还设置了标头来与 cors 错误进行协商。但是当我尝试在另一个文件 payment.js 上为发布请求设置标题时,我收到了 cors 错误。
我还在入口点 server.js 中导入了 payment.js。
我不明白出了什么问题。
提前谢谢你
Error I am getting
Access to XMLHttpRequest at 'http://192.168.43.147:4000/payment-check' from origin 'http://localhost:8100' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:4200' that is not equal to the supplied origin
这是我的入口点 server.js
const path = require('path');
const bodyParser = require('body-parser')
const app = express();
// parse requests of content-type - application/json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var cors = require('cors');
app.use(cors());
const routes = require('./payment/ordersapi');
app.use(routes);
const payments = require('./payment/payment');
app.use(payments);
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200', 'http://localhost:8100');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
payment.js // 另一个文件
const express = require('express');
const app = express();
const router = express.Router();
app.use('/', router);
router.post('/payment-check', function(req,res ) {
var options = req.body
console.log(options)
var payment_id = options.razorpay_payment_id
res.setHeader('payment_id' ,payment_id)
res.redirect('http://localhost:8100/order-success')
})
module.exports = router;
【问题讨论】:
-
浏览器在 devtools 控制台中记录的确切错误消息是什么?
-
我已编辑问题