自從用了react后,基本都是用fetch請求接口,在fetch封裝中,delete的處理一般是:
// 格式化get/delete請求的數據(fetch的get請求需要需要將參數拼接到url后面) if (option.method === 'get' || option.method === 'delete') { if (option.data) { url = url + formatUrl(option.data) } }
同事在axios的delete請求中遇到了如標題的問題,后台接收的數據類型是Content-Type使用application/x-www-form-urlencoded接收的,我原本想通過
let formData = new FormData(); // 當前為空 formData.append('id',1 );
整理成formData數據傳參,但后台接收到的依然是null。
於是通過別人分享的axios源碼分析:

也就是說,delete方法傳參,要么像get一樣,拼到url里面傳參,要么在config中傳參。
url中傳參就不說了,如何在config中傳參呢
這里就要注意了,數據格式要這樣包裝
{data:{id:1}}
不同於put和post傳參格式{id:1}
注意要跟后台統一body接收的參數數據類型是json對象還是json字符串。
