vue-resource如何进行跨域的数据请求

var demo=new Vue({

            el:'#app',

            data: {

                imgList:[],

                getImgUrl: ''    //存数据接口               

            },

            created: function(){

                this.getImg()              //定义方法

            },

            methods: {

                getImg: function(){

                  var that = this; 

               that.$http({           //调用接口

                        method:'POST',

                        url:"https://api.douban.com/v2/book/1220562"  //this指data

                    }).then(function(response){  //接口返回数据

                        this.imgList=response.data;                        

                    },function(error){

                    })

                }

            }

        })

写一个练手的demo 但是提示

https://img1.mukewang.com/5c70f08f0001ec8507690048.jpg

vue如何解决跨域问题呢


慕仙森
浏览 3683回答 2
2回答

饮歌长啸

跨域需要配置代理才可以。这和vue-resource没有关系。如果你是用的vue-cli搭建的项目,可以在dev-server.js文件中用express配置反向代理。网上有很多教程,也可以看http-proxy-middleware的github,讲的都非常通俗易懂。如果不是用vue-cli搭建的项目,需要引入express和http-proxy-middleware才可以配置代理,同样可以参考下面这个链接。https://github.com/chimurai/http-proxy-middleware
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript