首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >表单数据使用postman工作,但不在react、post方法、Springboot中工作

表单数据使用postman工作,但不在react、post方法、Springboot中工作
EN

Stack Overflow用户
提问于 2020-02-25 00:36:45
回答 1查看 236关注 0票数 0

我们使用的是带有springboot的react js。我们已经从邮递员表单数据中编写了服务和检查。它在工作,但当我们使用react js时,它不工作。Rest端点命中,但未将有效负载数据加载到服务。对于React,我们使用const formData = new FormData()并附加所有需要的输入。React代码

代码语言:javascript
运行
复制
const formData = new FormData();
formData.append("event", this.state.status);
formData.append("startDate", this.state.startDateTxt);
formData.append("sourceSystem", this.state.sourceSystem);
formData.append("endDate", this.state.endDateTxt);
formData.append("minPrice", this.state.minPrice);
formData.append("maxPrice", this.state.maxPrice);
httpRequest.open("POST", "http://localhost:8080/sa/searchData", true);
httpRequest.setRequestHeader("Content-Type","application/form-data");
httpRequest.onreadystatechange = () => {
    console.log("httpRequest.readyState", 
    httpRequest.readyState);
    if (httpRequest.readyState === XMLHttpRequest.DONE 
      && httpRequest.status === 200) {
        console.log("httpRequest.responseText ", httpRequest.responseText);
        updateData(JSON.parse(httpRequest.responseText));
    }
};
httpRequest.send(formData);

弹簧靴

代码语言:javascript
运行
复制
@PostMapping(value = "/sa/searchData")
public List<DataResponse> searchData(SearchCriteria searchCriteria) {
    return saService.searchData(searchCriteria);
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-26 00:32:35

错误代码'408'表明问题出在发送表单数据的react代码中。

当客户端向服务器发送了不完整的请求时,服务器会发送'408‘错误。

这个问题可以追溯到以下语句块

'httpRequest.open("POST",“本地主机:8080/sa/searchData”,true);...

此代码块正在打开与服务器的连接,但从未完成发送请求。

这会导致服务器在等待来自客户端的完整请求时超时,并最终发送408错误。

如何解决这个问题?

在.open()之后添加一个.send()方法来完成HTTP请求的发送。

一个工作代码片段的示例:

代码语言:javascript
运行
复制
httpRequest.open('POST', "localhost:8080/sa/searchData", true)
// add headers and other parameters
// Send the request
httpRequest.send()

更多信息:

https://malcoded.com/posts/react-http-requests-axios/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60380282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档