Fetch: POST json data

后端 未结 13 1750
小蘑菇
小蘑菇 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:18

    If your JSON payload contains arrays and nested objects, I would use URLSearchParams and jQuery's param() method.

    fetch('/somewhere', {
      method: 'POST',
      body: new URLSearchParams($.param(payload))
    })
    

    To your server, this will look like a standard HTML

    being POSTed.

提交回复
热议问题