getting Fetch response data(获取 Fetch 响应数据)
问题描述
我正在使用 chrome devTools 来镜像 webRequest.查看网络请求,响应中有一些我想要访问的 JSON 数据
I'm using the chrome devTools to mirror a webRequest. Looking at the network request, there is some JSON data in the response that I want to get access to
右键 --> 复制为 fetch -->
Right click --> Copy as fetch -->
fetch(
"https://www.url.com/service.svc?action=FindConversation&ID=-40&AC=1",
{
"credentials":"include",
"headers":{
"accept":"*/*",
"accept-language":"en-US,en;q=0.9",
"action":"FindConversation",
"content-type":"application/json; charset=UTF-8",
"actionid":"-40",
"unique_identifier":"062lCufCY0i5mI9NMTRUsF87XDq9ttYIonzZQjBcCOPvzoIJFOTSI6ZVNK9lMwy_iPFY2tuZzPY."
"x-requested-with":"XMLHttpRequest"
},
"referrer":"https://ballard.amazon.com/OWA/",
"referrerPolicy":"no-referrer-when-downgrade",
"body":"contains some body data I want to manipulate",
"method":"POST",
"mode":"cors"
}
).then(res => {console.log(res)})
这会打印出如下内容:
Response {type: "basic", url: "https://url/service.svc?action=FindConversation&ID=-40&AC=1", redirected: false, status: 200, ok: true, …}
body: ReadableStream
locked: false
__proto__: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "https://url/OWA/service.svc?action=FindConversation&ID=-40&AC=1"
__proto__: Response
当我检查我刚刚发出的网络请求时,它似乎没有返回任何 JSON 数据,而是以 200
代码响应.这正常吗?
When I inspect the network request I just made, it looks like it isn't returning any JSON data, but responds with a 200
code. Is that normal?
我要么期望它返回 JSON 数据,要么失败.
I either expected it to return JSON data or fail.
另外,JSON 响应数据在 res
中的什么位置?
Also, where would the JSON response data be in the res
?
推荐答案
这是正常现象.fetch()
返回一个 stream 对象,而不仅仅是正文.
This is normal behavior. fetch()
returns a stream object and not just the body.
使用 res.json()
提取 JSON 内容.对于非 JSON 响应,请使用 res.text()
Use res.json()
to extract the JSON content.
For non JSON responses, use res.text()
这篇关于获取 Fetch 响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 Fetch 响应数据
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01