2017-10-05 9400 views

回答

1

这不是一个真正的反应问题,因为获取然后是js本身的一部分。

获取返回一个对象作为无极包含类似的报头的各种信息,HTTP状态等等,等等

你有res.json()和各种其他的可能性。 .json()将仅以json内容的承诺返回主体。

欲了解更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

可以返回数据如下:

  • .arrayBuffer()
  • .blob()
  • .json()
  • .text()
  • .formData()
+0

很好,但是当'res'和'res.json()'@GottZ –

+0

之间存在'=>'符号时,这就是所谓的lambda。这也是JavaScript的一部分。去这里获得更多关于它的信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – GottZ

+0

@AniketSingh它基本上是'.then(function(res){return res。 json()})简而言之。 – GottZ

1

您的代码部分:

res => res.json() 

ES6 arrow function,其被翻译成:

function(res){ 
    return res.json(); 
} 

而且,关于json()功能:

json()方法正文mixin需要响应流和 将其读取完成。它返回一个承诺,将解析正文文本的结果作为JSON解析为 。

了解更多here

0

Javascript fetch函数异步地从指定的url中提取资源。同时fetch返回PromisePromise可以帮助执行异步部分,并在资源以获取的资源作为参数加载后运行传入thenres => res.json())的函数。如果获取的资源是JSON格式,则可以使用json()进行解析。

then还返回Promise使其可链接。

fetch(url) // asynchronously load contents of the url 
      // return a Promise that resolves when res is loaded 
     .then(res => res.json()) // call this function when res is loaded 
     // return a Promise with result of above function 
     .then(res => { // call this function when the above chained Promise resolves 
     this.setState({ 
      data: res, 
      error: res.error || null, 
      loading: false 
     }); 

res => res.json()也可以写为(but not exactly equal

function(res) { return res.json()}