No JSON object with fetch()

北战南征 提交于 2019-12-01 21:44:57

You almost got it right, you are missing one step though!

fetch doesn't return a json object, it returns a Response object, in order to get the json object, you have to use res.json()

fetch(`https://dribbble.com/oauth/token`, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          'client_id': 'MY_ID',
          'client_secret': 'MY_SECRET',
          'code': code
        })
      })
      .then((res) => {
        return res.json();
      })
      .then((json) => {
         console.log(json); // The json object is here
      });

It's a good practice to add a catch just in case something goes wrong.

.then((json) => {
      console.log(json); // The json object is here
 });
.catch((err) => {
     // Handle your error here.
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!