[英]How can I handle HTTP request with react front-end and flask back-end
I am building a web application using flask framework for my back-end and my front-end in react.我正在使用 Flask 框架为后端和前端构建一个 Web 应用程序。 I would like to connect these two using
http
request but I have no clue on where to start.我想使用
http
请求连接这两个,但我不知道从哪里开始。 I am using PyCharm IDE for both front and back end.我在前端和后端都使用PyCharm IDE 。
I have looked into react-http-request
library but I didn't understand it.我查看了
react-http-request
库,但我不明白。
I want to be able to send strings from my front-end to my back-end and send json
files from back-end to front-end.我希望能够将字符串从前端发送到后端,并将
json
文件从后端发送到前端。
Native fetch() function of the browser can do that for you.浏览器的本机 fetch() 函数可以为您做到这一点。
Use it like this:像这样使用它:
fetch('http://your.server.domain/api/endpoint', {
method: 'GET',
headers: {"Content-Type": "application/json"},
}).then((res) => {
return res.json();
}).then((json) => {
// this is your json data from your server
console.log(json);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.