How to make a rest post call from ReactJS code?

前端 未结 9 1934
一生所求
一生所求 2020-12-02 05:28

I am new to ReactJS and UI and I wanted to know how to make a simple REST based POST call from ReactJS code.

If there is any example present it would be really h

9条回答
  •  离开以前
    2020-12-02 05:54

    Straight from the React docs:

    fetch('https://mywebsite.com/endpoint/', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        firstParam: 'yourValue',
        secondParam: 'yourOtherValue',
      })
    })
    

    (This is posting JSON, but you could also do, for example, multipart-form.)

提交回复
热议问题