React在PUT时发送一个空JSON对象
我试图向我创建的REST服务器发送PUT请求,但无论我做什么,发送的JSON对象都是空的。我已经在Postmaster中尝试过该服务器,它可以正常工作,所以问题一定出在React应用程序上 这是发送请求的函数:React在PUT时发送一个空JSON对象,json,reactjs,rest,axios,Json,Reactjs,Rest,Axios,我试图向我创建的REST服务器发送PUT请求,但无论我做什么,发送的JSON对象都是空的。我已经在Postmaster中尝试过该服务器,它可以正常工作,所以问题一定出在React应用程序上 这是发送请求的函数: handleSubmit(e) { e.preventDefault(); const newBrand = this.state.brand newBrand.description = this.state.editorStat
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":[]}
componentDidMount() {
Axios.get("http://localhost:8080/BackendWiki/api/brands/")
.then(res => {
const brands = res.data;
this.setState({brands})
})
}
我已经实现了一个get请求,它的工作方式应该是这样的:
{"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":[]}
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不起作用。任何帮助都将不胜感激。您是否尝试过在不字符串化的情况下发送它?您正在将json转换为字符串,然后将其放入带有
{json}
的对象中。这里有点不对劲。您的api接受表单编码数据还是json?您可能需要对其进行表单编码,或者只发送json正文而不使用stringify。当我不使用stringify时,会得到相同的结果。我的API接受json,我用处理请求的代码更新了问题。我是否需要在请求中设置一个头来指定它是json?您是否尝试过在不进行字符串化的情况下发送它?您正在将json转换为字符串,然后将其放入带有{json}
的对象中。这里有点不对劲。您的api接受表单编码数据还是json?您可能需要对其进行表单编码,或者只发送json正文而不使用stringify。当我不使用stringify时,会得到相同的结果。我的API接受json,我用处理请求的代码更新了问题。我是否需要在请求中设置一个头来指定它是json?