reactjs - Spring Boot:从表单(reactjs)提交中获取空输入
问题描述
我有一个基本的表单提交 Spring Boot 应用程序。我的问题是当我提交表单时,我得到空结果。
这是我的表单代码:
- 主题控制.java
package io.group.artifact.MainApplication.TopicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
@CrossOrigin(origins = "http://localhost:3000")
@RestController
public class TopicControl {
@Autowired
private TopicService topicService;
@RequestMapping("/topics")
public List<Topic> getTopic(){
return topicService.getAllTopics();
}
@RequestMapping("/topics/{id}")
public Topic getTopic(@PathVariable String id) {
return topicService.getTopic(id);
}
@RequestMapping(method= RequestMethod.POST, value = "/topics")
public void addTopic(@RequestBody Topic topic){
topicService.addTopic(topic);
}
@RequestMapping(method= RequestMethod.PUT, value = "/topics/{id}")
public void updateTopic(@RequestBody Topic topic, @PathVariable String id){
topicService.updateTopic(id, topic);
}
@RequestMapping(method= RequestMethod.DELETE, value = "/topics/{id}")
public void deleteTopic(@PathVariable String id){
topicService.deleteTopic(id);
}
}
这段代码是我的反应部分:
- CreateEmployeeComponent.jsx
saveEmployee = (e) => {
e.preventDefault();
let employee = {firstName: this.state.firstName, iD: this.state.iD, desc: this.state.desc};
console.log('employee => ' + JSON.stringify(employee));
EmployeeService.createEmployee(employee).then(res =>{
this.props.history.push('/topics');
});
}
这也是前端代码
- EmployeeService.js
import axios from 'axios';
const EMPLOYEE_API_BASE_URL = "http://localhost:8080/topics";
class EmployeeService {
getEmployees(){
return axios.get(EMPLOYEE_API_BASE_URL);
}
createEmployee(employee){
return axios.post(EMPLOYEE_API_BASE_URL, employee);
}
}
export default new EmployeeService()
在 http://localhost:8080/topics 我得到以下输出:
解决方案
通过查看此代码,来自前端的参数和请求正文 DTO 中的字段是不同的,这是错误的,因为两者都应该匹配,然后才会起作用。
前端 :
名字,ID,描述
后端:
名称、ID、描述
确保这些在双方都是相同的。
推荐阅读
- php - 修复在 php 版本 7.2.4 中安装 inoERP 时出现的 Deprecated Function 错误
- java - 剖析来自服务器的响应以获得您需要的内容
- json - 包含大量内容的 Oracle json_object 导致 ORA-40459
- android - 哪些属性会自动保存,无需额外编码?屏幕旋转后,LinearLayout 中的背景颜色未恢复
- html - 我想改变visualViewport的比例。我怎样才能务实地实现这一目标?
- android - 错误在构建 ionic android 应用程序时无法读取 null 的属性“1”
- php - Dockerized Laravel 应用程序,资产总是 404
- .net - MsBuild 目标不起作用
- regex - 如何返回变量并在 Python 类中的其他函数/方法中重用它们
- c++ - LibXL:excel 跨表公式未更新