如何将请求的JSON消息转换为可用数据? [英] How do I convert a requested JSON message to usable data?

查看:107
本文介绍了如何将请求的JSON消息转换为可用数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个烧瓶服务器正在运行,当我手动访问127.0.0.1:5000/data/get_data时,它会向我提供JSON消息. 在此flask服务器中,CORS被禁用,并且在我的提取请求中,我还添加了{mode: 'no-cors'}.

每当我在javascript中使用fetch-request时,响应都为空,而Flask告诉我听到了GET请求,并发送了响应. 当我手动请求数据时,Flask会给我完全相同的输出(并且我可以在浏览器中查看接收到的JSON).

I have a flask server running that provides me with a JSON message when I manually visit 127.0.0.1:5000/data/get_data. CORS is disabled in this flask server and in my fetch-request I added {mode: 'no-cors'} as well.

Whenever I use the fetch-request in javascript, the response is empty while Flask tells me the GET request was heard and a response was sent. Flask gives me the exact same output when I manually request the data (and I can view the received JSON in the browser).

这是执行提取请求的javascript代码

This is the javascript code to do the fetch-request

fetch('http://127.0.0.1:5000/data/get_data', {
    mode: 'no-cors'
})
.then(response => console.log(response));


FLASK清楚地获得了请求并发送了响应:
此图像是来自FLASK的INFO消息


FLASK clearly gets the request and sends a response:
this image is the INFO message from FLASK

此图像是来自FLASK的HTTP响应

FETCH请求收到的内容(登录到firefox开发者控制台)

我很想知道如何接收flask发送的数据,并将其保存在javascript字典中.

what the FETCH-request receives (logged in firefox dev-console)

I would love to know how I can receive the data that flask sends out, and keep it in a dictionary in javascript.

推荐答案

fetch('http://127.0.0.1:5000/data/get_data', {
    mode: 'no-cors'
})
.then(response => response.json())
.then(json => console.log(json));

这篇关于如何将请求的JSON消息转换为可用数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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