How to make a rest post call from ReactJS code?

前端 未结 9 1936
一生所求
一生所求 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:53

    I think this way also a normal way. But sorry, I can't describe in English ((

        submitHandler = e => {
        e.preventDefault()
        console.log(this.state)
        fetch('http://localhost:5000/questions',{
            method: 'POST',
            headers: {
                Accept: 'application/json',
                        'Content-Type': 'application/json',
            },
            body: JSON.stringify(this.state)
        }).then(response => {
                console.log(response)
            })
            .catch(error =>{
                console.log(error)
            })
        
    }

    https://googlechrome.github.io/samples/fetch-api/fetch-post.html

    fetch('url/questions',{ method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(this.state) }).then(response => { console.log(response) }) .catch(error =>{ console.log(error) })

提交回复
热议问题