json - React 在 PUT 上发送一个空的 JSON 对象
问题描述
我正在尝试向我创建的 REST 服务器发送 PUT 请求,但无论我做什么,发送的 JSON 对象都是空的。我已经在 Postmaster 中尝试过服务器,它按预期工作,所以问题一定出在 React 应用程序上。
这是发送请求的函数:
handleSubmit(e) {
e.preventDefault();
const newBrand = this.state.brand
newBrand.description = this.state.editorState.getCurrentContent().getPlainText()
const json = JSON.stringify(newBrand)
console.log(json)
axios.put(`http://localhost:8080/BackendWiki/api/brands/${this.props.brandName}`, {json})
.then(res => {
console.log(res);
console.log(res.data);
})
}
打印到控制台的子对象如下所示:
{"id":56,"created":1597255927810,"updated":1597255927810,"links":[{"href":"http://localhost:8080/BackendWiki/api/brands/rolex","rel":"self","action":"GET"},{"href":"http://localhost:8080/BackendWiki/api/brands/rolex","rel":"self","action":"DELETE"},{"href":"http://localhost:8080/BackendWiki/api/brands/rolex","rel":"self","action":"PUT"}],"name":"Rolex","founded":0,"founder":null,"ceo":null,"headQuarters":null,"description":"more, more, more","text":[]}
我已经实现了一个正常工作的 get 请求,如下所示:
componentDidMount() {
Axios.get("http://localhost:8080/BackendWiki/api/brands/")
.then(res => {
const brands = res.data;
this.setState({brands})
})
}
下面是处理 PUT 请求的代码:
@PUT
@Path("/{name}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateBrand(@PathParam("name") String name, Brand updateBrand) {
System.out.println(updateBrand);
Brand oldBrand = brandDAO.getBrandByName(name.replaceAll("-", " "));
if (oldBrand != null) {
updateBrand.setId(oldBrand.getId());
brandDAO.update(updateBrand);
return Response
.status(200)
.entity(brandDAO.getBrandById(updateBrand.getId()))
// .header("Access-Control-Allow-Origin", "*")
.build();
} else {
return Response
.status(404)
//.header("Access-Control-Allow-Origin", "*")
.build();
}
}
我也没有收到任何错误消息,所以我对为什么 PUT 不起作用感到有些困惑。任何帮助将非常感激。
解决方案
推荐阅读
- r - 条件过滤,其中过滤器变量及其相关数据点在每行中更改列
- python - 无法在 Heroku 上推送 django 应用程序,因为它收集了所有模块
- python - Discord Bot clear 命令在我添加到其他服务器后不执行 | Python
- python - 如何在多个条件下转换合并两个数据框?
- linux - 为什么 mprotect 是与 mmap 不同的系统调用
- r - cor.test.default(x = mat[, i], y = mat[, j], ...) 中的错误:没有足够的有限观察
- javascript - 显示对 a 的所有更改
我在一个网站项目中工作,它想显示我们在
<input type="text" id="input" />
标签中编辑的内容。但问题是结果只显示在
<div class="result"></div>
.我想在
- angular - Google Picker 并将所选文件保存在 Angular 8 的文件夹中
- python - tensorFlow 向我抛出错误 ValueError: Layersequential Expects 1 个输入,但它收到 2 个输入张量
- python - 算法 X Python 实现