Proper Way to Make API Fetch 'POST' with Async/Await

后端 未结 3 1641
难免孤独
难免孤独 2020-12-29 06:00

I\'m working on a project that requires me to make requests to an API. What is the proper form for making a POST request with Async/Await?

As an example, here is my

3条回答
  •  猫巷女王i
    2020-12-29 06:55

    Here is an example with configuration:

    try {
        const config = {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
        }
        const response = await fetch(url, config)
        //const json = await response.json()
        if (response.ok) {
            //return json
            return response
        } else {
            //
        }
    } catch (error) {
            //
    }
    

提交回复
热议问题