从获取中获取数据->承诺->回复 [英] Get the data from fetch -> promise -> response

查看:74
本文介绍了从获取中获取数据->承诺->回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据发布到服务器,但是我不知道如何获取响应数据.

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

我有以下代码:

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

它打印一个Response,其中包含诸如body(ReadableByteStream),bodyUsed(false),ok(true),status(200)等数据. 但我无法找到返回的数据.当我打开chrome开发者控制台-网络时,我在那里看到了响应数据.

It prints a Response it contains data such as body (ReadableByteStream), bodyUsed (false), ok (true), status (200),... but I cannot find the data I get back, nowhere. When I open the chrome developer console - network I see the response data there.

我在做什么错了?

我一直在寻找一些资源,如何获取,承诺,...如何工作,但是我找不到写得很好的东西.

I've been looking for some resources how fetch, promises,... work but I couldn't find any well written.

推荐答案

您还可以在获取响应中调用其他方法,例如.json().blob().这些方法返回一个Promise,您可以在其上调用.then().

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)
    })

查看有关使用访存的一些文档,以及有关

这篇关于从获取中获取数据->承诺->回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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