鍍金池/ 問答/HTML/ vue-resource如何進行跨域的數(shù)據(jù)請求

vue-resource如何進行跨域的數(shù)據(jù)請求

var demo=new Vue({
            el:'#app',
            data: {
                imgList:[],
                getImgUrl: ''    //存數(shù)據(jù)接口               
            },
            created: function(){
                this.getImg()              //定義方法
            },
            methods: {
                getImg: function(){
                  var that = this; 
               that.$http({           //調(diào)用接口
                        method:'POST',
                        url:"https://api.douban.com/v2/book/1220562"  //this指data
                    }).then(function(response){  //接口返回數(shù)據(jù)
                        this.imgList=response.data;                        
                    },function(error){
                    })
                }
            }
        })

寫一個練手的demo 但是提示

clipboard.png

vue如何解決跨域問題呢

回答
編輯回答
編輯回答
糖豆豆

現(xiàn)在都不用vue-resource了,用axios。
跨域的話要么在后臺 header 加 Access-Control-Allow-Origin 允許跨域,要么在前端工程化的vue-cli proxyTable 允許跨域訪問 http://www.jianshu.com/p/95b2...

2018年1月27日 08:07
編輯回答
情殺

跨域需要配置代理才可以。這和vue-resource沒有關系。如果你是用的vue-cli搭建的項目,可以在dev-server.js文件中用express配置反向代理。網(wǎng)上有很多教程,也可以看http-proxy-middleware的github,講的都非常通俗易懂。如果不是用vue-cli搭建的項目,需要引入express和http-proxy-middleware才可以配置代理,同樣可以參考下面這個鏈接。
https://github.com/chimurai/http-proxy-middleware

2017年3月28日 13:34
編輯回答
拽很帥

這個要在后臺在 header 加 Access-Control-Allow-Origin 允許跨域

2017年10月14日 14:18