Fetch: POST json data

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

    It might be useful to somebody:

    I was having the issue that formdata was not being sent for my request

    In my case it was a combination of following headers that were also causing the issue and the wrong Content-Type.

    So I was sending these two headers with the request and it wasn't sending the formdata when I removed the headers that worked.

    "X-Prototype-Version" : "1.6.1",
    "X-Requested-With" : "XMLHttpRequest"
    

    Also as other answers suggest that the Content-Type header needs to be correct.

    For my request the correct Content-Type header was:

    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"

    So bottom line if your formdata is not being attached to the Request then it could potentially be your headers. Try bringing your headers to a minimum and then try adding them one by one to see if your problem is rsolved.

提交回复
热议问题