Skip to content

关于跨域请求的优化(减去node.js繁琐操作) #1

@heightzhang

Description

@heightzhang

原理:vue-cli跨域环境配置 index.js

dev: {
    env: require('./dev.env'),
    port: 8080,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
        "/music":{
            target:'http://m.kugou.com',
            changeOrigin:true, //=>服务器代理true;
            pathRewrite:{
                '^/music':''
            }
        }
    }

用法:请求示列:

 axios.get('/music/plist/index&json=true')  //实际解析地址=>http://m.kugou.com/plist/list/128180?json=true
                 .then((response) => {
                   this.info = response.data.plist.list.info.map(function(item){
                       item.imgurl =  item.imgurl.replace('{size}','400')
                        return item
                   });
                   console.log(this.info)

                })
                .catch((error) => {
                    console.log(error);
                });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions