fetch请求方法传参格式和headers有关

时间:2025-04-26 14:28:46

1、如果headers请求头为'Content-Type': 'application/json',则参数body:  ({a:1,b:{c:2,d:3}});

2、如果headers请求头为"Content-Type": "application/x-www-form-urlencoded" ,则参数body:  "a=1&b=2&c=3",传字符串格式,这里必须用=拼接,冒号拼接会报错!

第二种情况举例如下:

let options = {
                headers: {
                    // 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
                    "Content-Type": "application/json"
                }, // 设置请求头
                mode:'no-cors',
                method:'post',
                // body:`user_id=${userID}`, // 参数
                body: ({user_id: userID}) // 参数
            }
            ('option', options)
            const res = await fetch(url, options)
            const data = await ()
            const result =
            return result