鍍金池/ 問答/HTML/ axios跨域問題

axios跨域問題

1.我在main.js中設置了

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

在其他組件用的時候一旦帶參數(shù)例如:

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之后傳過去的參數(shù)是{'offset':5}這種形式,我想要像ajax那樣直接offset:5,不包含在對象里面,如何實現(xiàn),要加什么配置選項

題目描述

題目來源及自己的思路

相關代碼

// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)

你期待的結果是什么?實際看到的錯誤信息又是什么?

回答
編輯回答
愛是癌
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)
  }],
2017年11月21日 08:49