express+vue做跨域请求

可以改vue,也可以改express的接口层;

改vue(/config/index.js):

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    //利用proxyTable我们能够将外部的请求通过webpack转发给本地,也就能够将跨域请求变成同域请求了。
    proxyTable: {
      '/api':{
        target: 'http://localhost:3000/',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/api'
        }
      }
    },

改接口(这种才是核心)

var app = express();

app.all('/api', 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();
});

未经允许不得转载:朱邦邦的博客 » express+vue做跨域请求

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址