Get the data from fetch -> promise -> response

前端 未结 1 657
既然无缘
既然无缘 2021-02-07 11:27

I am trying to post some data to the server but I don\'t know how to get back the response data.

I have the following code:

fetch(url, {
  method: \'POST         


        
相关标签:
1条回答
  • 2021-02-07 11:58

    There are further methods you call on a fetch response, such as .json(), or .blob(). These methods return a promise which you can call .then() on.

    fetch(url, {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            email: login,
            password: password,
        })
    })
        .then(function (a) {
            return a.json(); // call the json method on the response to get JSON
        })
        .then(function (json) {
            console.log(json)
        })
    

    Check out some documentation on using fetch, and some other documentation on how the response object works in a fetch call.

    0 讨论(0)
提交回复
热议问题