Fetch: POST json data

后端 未结 13 1435
小蘑菇
小蘑菇 2020-11-22 03:25

I\'m trying to POST a JSON object using fetch.

From what I can understand, I need to attach a stringified object to the body of the request, e.g.:



        
13条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 04:11

    I think your issue is jsfiddle can process form-urlencoded request only.

    But correct way to make json request is pass correct json as a body:

    fetch('https://httpbin.org/post', {
      method: 'post',
      headers: {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({a: 7, str: 'Some string: &=&'})
    }).then(res=>res.json())
      .then(res => console.log(res));

提交回复
热议问题