Vue 使用 http和https问题(以及跨域问题)

在Vue项目的config/index.js 里面添加代码:

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    // =================== 该属性为添加内容 start====================
    proxyTable: {  
        '/queue-admin': {  // 使用"/queue-admin"来代替 API头部,可随意命名
            //target: 'http://192.168.11.12:8080',  // 源地址 本地开发的API头部
            target: "https://xxx.com",  // 配置到服务器后端的API头部
            pathRewrite: { 
                '^/queue-admin': '/queue-admin'  // 路径重写,第一个与上面相同,第二个/queue-admin 为server.context-path(服务器的上下文)
            },
            // 以下解决https 访问问题。上面的可以访问http
            changeOrigin: true,
            secure: false,
            headers: {
              Referer: 'https://xxx.com'
            }
        }
    },

    // =================== 该属性为添加内容 end====================

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8082, // 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-
.........

之后可在不同页面的js中通过以下方法调用PAI:

$.ajax({
	type: 'post',
	url: '/queue-admin/xxx/xxx/xxx',  // /queue-admin已实现代理问题,后面为Controller的RequestMapping 路径
	contentType: 'application/json',
	data:JSON.stringify({}),
	dataType: 'json',
	success: function(data){
		console.log(data)
	},
    fail: function(data){
		console.log(data)
	}
});

猜你喜欢

转载自blog.csdn.net/LZD30/article/details/81946752