<br /><br />module.exports = function (app) {
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.get('/getdata', function(req, res) {
// res.send({id:req.params.id, name: req.params.password});
// res.send("hello");
res.json("hello");
});
}<br /><br />const express = require('express')
const app = express()
const api = require('./api')
api(app)
app.listen(8889)
console.log("listen on port 8889");<br />$.ajax({
dataType: 'json',
url: 'localhost:8889/server',
type:'get',
success: function(data){
console.log(data);
}
});node后台,前端nginx服务器ajax访问,出现跨域问题,使用nginx反向代理