vue项目中用了axios的get请求,返回的状态码是200,浏览器有数据,但是怎样获取?

新手上路,请多包涵

在vue项目中,用的是axios的get方法请求 金山词霸每日一句api
返回的状态码是200,而且有响应数据,如图:

clipboard.png

clipboard.png

但是浏览器却报跨越拦截,如图:

clipboard.png

然后index.js文件中dev的设置:

proxyTable: {
      '/api':{
        target:"https://open.iciba.com/",
        changeOrigin:true,
        pathRewrite: {'^/api' : '/'}
      }
    },

接着是组件Cart.vue的部分代码:

// 放在mounted函数里的 axios 拦截器
// request 
this.$axios.interceptors.request.use((config)=>{
    config.withCredentials = true;
    console.log("request init...");
    console.log(config)
    config.headers={
        "Content-Type":"application/x-www-form-urlencoded"
    }
    return config;
})
// response 
this.$axios.interceptors.response.use((response)=>{
    console.log("response init...");
    console.log(response);
    return response;
})
// axios的get请求
methods: {
        get(){
            var me = this;
            var date =  new Date();
            var year = date.getFullYear() + '-';
            var month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() +1) + '-';
            var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            var now = year+month+day;
                
            me.$axios.get('/dsapi',{
                params:{
                    date:now // 年-月-日
                }
            }).then(res=>{
                console.log(res)
            })
        },
}

这是怎么回事,浏览器的network查看得到数据,却无法获取,求解决方法

阅读 6.9k
1 个回答

就是跨域了, 前端可以用jsonp请求

import jsonp from 'jsonp';

jsonp('https://open.iciba.com/dsapi/?date=2018-07-11', null, (err, data) => {
  console.log(data)
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题