首页 文章 Vue解决ajax 跨域请求,无需后台(php)改返回信...

Vue解决ajax 跨域请求,无需后台(php)改返回信息header头

发布时间:2019-07-05 15:40:10 作者:Soul-Yang 阅读量:707

开启 proxyTable,注意此方法只能在开发环境起作用,build之后的项目依然无法跨域,后面会介绍修改apache/nginx的url重写(或反向代理)实现跨域。

此方法以 vue脚手架生成的标准项目为准。一般在项目config目录下面有个index文件。里面格式如下:

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')

module.exports = {
dev: {

// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target:'http://www.test.com:8080',//不填端口将默认80
// secure: false, // 如果是https接口,需要配置这个参数
changeOrigin:true,
pathRewrite:{
'^/api': ''
}
}
},

// Various Dev Server settings
host: 'myvue.com', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

// Use Eslint Loader?
// If true, your code will be linted during bundling and
// linting errors and warnings will be shown in the console.
useEslint: true,
// If true, eslint errors and warnings will also be shown in the error overlay
// in the browser.
showEslintErrorsInOverlay: false,

/**
* Source Maps
*/

// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',

// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,

cssSourceMap: true
},

build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),

// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',

/**
* Source Maps
*/

productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',

// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],

// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
}

上面配置中,我们根据实际情况只需要修改proxyTable对应配置即可。假设我后端请求地址是http://www.test.com:8080,所有api的接口url都以/api开头。所以首先需要匹配所有以/api开头的.然后修改target的地址为http://www.test.com:8080。即修改pathRewrite配置。再次注意:这个方式只能在开发环境中使用。


bulid之后的项目,修改ngnix配置,添加如下(反向代理),当然也可以配置重写方式,下边给出反向代理配置:

location /api/ {

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded $proxy_add_x_forwarded_for;

    proxy_pass http://www.test.com/;

}

注释:其实只需  proxy_pass  http://www.test.com/;   一句就行;前面两句可以使后台程序获取前端访问者的真实IP地址,具体的原理自行查阅资料。



  
留言
https://blog.key9.cn/
用户登录
您还没有写任何评论内容!
您已经评论过了!
只能赞一次哦!
您已经收藏啦!