How do I convert a requested JSON message to usable data?

非 Y 不嫁゛ 提交于 2020-07-10 07:09:18

问题


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

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 clearly gets the request and sends a response:
this image is the INFO message from FLASK

this image is the HTTP response from FLASK

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.


回答1:


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


来源:https://stackoverflow.com/questions/59289884/how-do-i-convert-a-requested-json-message-to-usable-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!