作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个评论应用程序。我正在使用 json-server 进行发布请求。我的数据库看起来像这样
[{
"id": 5,
"name": "Tom Cruise",
"image": "http://placeholder.pics/svg/300x200/000000",
"title": "Hiring Manager",
"employeeId": "22222222",
"funFact": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"dateOfJoining": "04/12/2013"
},
{
"id": 6,
"name": "Julius Caesar",
"image": "http://placeholder.pics/svg/300x200/fdfdfd",
"title": "Sales Executive",
"employeeId": "33333333",
"funFact": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"dateOfJoining": "04/12/2016"
}
]
这是我的添加 Action
//Add Employee
export const addEmployee = employeeData => dispatch => {
axios
.post("http://localhost:3004/employees", {
employeeData,
headers: { "Content-Type": "application/json" }
})
.then(response =>
dispatch({
type: ADD_EMPLOYEE,
payload: response.data.employeeData
})
)
.catch(err => console.log(err));
};
这是我的 reducer
case ADD_EMPLOYEE:
return {
...state,
employees: [action.payload, ...state.employees],
loading: false
};
一切正常。问题是它在数据库中的存储方式是这样的
{
"employeeData": {
"name": "Manpreet Sandhu",
"title": "bootstrap 4",
"funFact": "cccZCZCZ",
"image": "CZCZCZC",
"employeeId": "ZCZCZC",
"dateOfJoining": "C"
},
"headers": {
"Content-Type": "application/json"
},
"id": 7
}
我想删除在每个条目之前添加的“employeeData”。
最佳答案
引用axios document ,
axios.post(url[, data[, config]])
在你的行动中,
//...
axios
.post("http://localhost:3004/employees", {
employeeData,
headers: { "Content-Type": "application/json" }
})
//...
您已将 headers: { "Content-Type": "application/json"}
放入 data
参数中
我猜,你会做的,应该是这样的
//...
axios
.post("http://localhost:3004/employees",
employeeData, //data
{ headers: { "Content-Type": "application/json" } } //config
)
// or
axios({
method: 'post',
url: 'http://localhost:3004/employees',
data: employeeData,
headers: { "Content-Type": "application/json" }
})
//...
关于javascript - react /减少 : Not saving in state as intended,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789122/
我是一名优秀的程序员,十分优秀!