使用fetch发起POST请求

275 阅读1分钟

fetch发起POST请求,传递数据时,需要使用JSON.stringigy()进行转换成字符串之后,再把数据发送给服务端。

const data = {}
JSON.stringify(data)
 fetch("https://example.com",{
    method:"POST",
    body:JSON.stringify(data),

})
.then(res=>res.json()).then(data=>console.log(data))

错误案例:


const data = {}
JSON.stringify(data)
 fetch("https://example.com",{
    method:"POST",
    body:data,
})
.then(res=>res.json()).then(data=>console.log(data))