后端同事提供的接口是get請求,需要通過body傳值,在postman里面是可以正常請求的,但是使用axios的時候卻參數總是不正確。
后來查了相關文檔,get請求是沒有請求主體的,axios封裝的get請求也不會把主體(body)傳給后端,如果需要傳遞主體的話,就得post請求或者其他。
改成post請求就可以正常請求后端數據了。
let config = {
method: 'post',
url: 'https://example.com/home',
data: [1, 2]
};
axios(config)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
});
參考鏈接:
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Methods/GET
