[工程架构思考-请求] request-header-之-Content-Type-application-json

46 阅读1分钟

如果没有设置header的conten-type, 在request payload中传递了对象,浏览器会自动将content-type改成application/json;

有些特殊情况:比如有的时候,request payload 为空,这个空其实不是 null, undefined, 而是服务器端没要求传参数,也应该要把 request payload 写成 {}, 否则会导致: content-type: json 即使强制添加,也会被浏览器移除。导致出现 415 Response Status Code

image

fetch('url', {
    method: 'POST',
    body: {
        xxx:xxx,
    },
})

如果是用了formdata,浏览器也可以很好的识别出请求的content-type,

const body = new FormData()
body.append('xxx', 'xxx')
fetch('url', {
    method: 'POST',
    body,
})