javascript - 您可以对反应组件进行 REST 调用吗?
问题描述
不要在服务器上调用 REST API,而是对 react js 组件进行休息调用。例如:
称呼
import React from 'react';
function Post(props) {
function fetchPost() {
fetch(`http://localhost:3000/fetch_post/${props.match.params.post_id}`,
{
method: "GET",
}
)
.then((res) => {
if (!res.ok) {
return res.json().then(e => Promise.reject(e));
}
return res.json();
})
.then((res) => {
console.log('returned: ' + res);
})
.catch(err => {
console.log('error: ' + err);
});
}
}
...
来自 React 组件的 REST API:
import React from 'react';
function FetchPost(props) {
return (
// How do I return a json object (obviously using JSON.stringify)? This return expects an HTML element
);
}
export default FetchPost;
这可能吗?我删除了所有不重要的数据。
解决方案
推荐阅读
- python - 理解这个 kivy 程序的确切错误告诉我的问题
- linux - RAD SERVER 10.4.1 “E2597(SystemPath)\ld-linux.exe: 错误: 找不到 -lz” 部署到 Linux 64
- python - 我正在尝试在 Python 中运行这个程序,但我无法执行 def
- java - 从 unix 命令行运行 jar 文件
- cuda - Cuda GPUassert:遇到非法内存访问
- node.js - CircleCI 在 fs-extra node_modules 失败
- jquery - 基础 XML(使用 Jquery 提取)
- python - raise ValueError("Input contains NaN") ValueError: Input contains NaN 在尝试构建机器学习模型时
- flutter - 从 Flutter 中打开 Google 地图并选择地点
- connection - MULE-4:记录 SFTP 登录尝试