fetch post application/json 传递复杂类型

使用原始的
application/x-www-form-urlencoded,body需要是a=1&b=2&c=3 格式,无法传递复杂多层次的对象

  fetch('/test/post', {
        headers: {
           "Content-Type": "application/x-www-form-urlencoded" 
        },
        method: 'POST',
        body:  "a=1&b=2&c=3"
            })

通过使用application/json ,可以直接传递JSON字符串,post复杂类型

    fetch('/test/post', {
        headers: { 
            "Content-Type": "application/json"
        },
        method: 'POST',
        body:  JSON.stringify({a:1,b:{c:2,d:3}})
    })

你可能感兴趣的:(javascript)