获取 response.json() 给出 responseData = undefined [英] fetch response.json() gives responseData = undefined

查看:93
本文介绍了获取 response.json() 给出 responseData = undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 fetch 时:

When using fetch:

  fetch(REQUEST_URL, {
      method: 'get',
      dataType: 'json',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    })
    .then((response) => 
      {
        response.json() // << This is the problem
      })
    .then((responseData) => { // responseData = undefined

        console.log(responseData);
     });
     }).catch(function(err) {
        console.log(err);
      })
     .done();

以下作品有效,你知道为什么吗?:

The following works works, do you know why? :

    JSON.parse(response._bodyText)

推荐答案

链接响应应该更像这样,特别是 response.json 部分.然后你应该在 console.log 中得到一个 Object.

The chaining response should look more like this, specifically the response.json part. Then you should get an Object back in console.log.

.then(response => response.json())
.then(response => {

    console.log(response)

}

这篇关于获取 response.json() 给出 responseData = undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆