axios跨域问题

1.我在main.js中设置了

axios.defaults.withCredentials=true
axios.defaults.crossDomain=true
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

在其他组件用的时候一旦带参数例如:

self.axios.post('http://pj.dianmila.com/supersolid/supersolid_api.php?a=list',{offset:self.swiperlen},{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(res) {
                        var datajson = res.data;
..................
                            }
                        } 
                    }).catch(function (error) {
                    alert('未能与服务器连接,请稍后尝试')
                console.log(error);
            });

不加后面的headers的设置就不能跨域了为什么求教

2.当我设置了headers之后传过去的参数是{'offset':5}这种形式,我想要像ajax那样直接offset:5,不包含在对象里面,如何实现,要加什么配置选项

题目描述

题目来源及自己的思路

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 1.5k
1 个回答
axios.post('http://pj.dianmila.com/supersolid/supersolid_api.php?a=list',{offset:1},{
  headers: {'Content-Type':'application/x-www-form-urlencoded'},
  transformRequest: [function (data) {
    return Qs.stringify(data)
  }],
}).then(function(res) {
    console.log(res)
}).catch(function (error) {
    console.log(error);
})

transformRequest: [function (data) {
    return Qs.stringify(data)
  }],
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题