我想在localhost:3030/index.html里获取localhost:8080/data.json的内容,可是如何在localhost:8080里设置允许跨域?在node里我写了res.header('Access-Control-Allow-Origin:*'),但还是无法跨域,这该如何解决?是否有文档或者实际demo可供查看?
已解决。。
var express = require('express');
var app = express();
var router = express.Router();
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
next();
});
app.get('/', function (req, res) {
res.send('Hello World');
})
router.post('/data.json',function (req,res,next) {
res.setHeader('Access-Control-Allow-Origin', '*');
});
app.use(express.static(__dirname + '/static'));
var server = app.listen(8080, function () {
})
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
https://github.com/expressjs/...
var req = http.request(options, function(res) {})
把option换成localhost:8080 然后自己在控制器里面接收localhost:3030/index.html传递的参数 把这个参数再传递给http.request的请求里面