TypeError:JSON.stringify(…)。then不是一个函数
我是一个新的反应和有问题。我想做的是将部门添加到通过VisualStudio中创建的web api创建的表中。我想通过模式弹出窗口添加部门。然而,我有一个问题。当我点击添加按钮时,它会给我这个错误TypeError:JSON.stringify(…)。then不是一个函数,json,reactjs,react-redux,react-bootstrap,webapi,Json,Reactjs,React Redux,React Bootstrap,Webapi,我是一个新的反应和有问题。我想做的是将部门添加到通过VisualStudio中创建的web api创建的表中。我想通过模式弹出窗口添加部门。然而,我有一个问题。当我点击添加按钮时,它会给我这个错误 TypeError: JSON.stringify(...).then is not a function handleSubmit D:/React/employee-app/src/components/AddDepModal.js:22 19 | 'Content-Ty
TypeError: JSON.stringify(...).then is not a function
handleSubmit
D:/React/employee-app/src/components/AddDepModal.js:22
19 | 'Content-Type':'application/json'
20 |
21 | },
> 22 | body: JSON.stringify({
| ^ 23 | DepartmentID:null,
24 | DepartmentName: event.target.DepartmentName.value
25 | })
View compiled
这是我的代码,我在这里调用'POST'方法
export class AddDepModal extends Component{
constructor(props){
super(props);
}
handleSubmit(event){
event.preventDefault();
fetch('https://localhost:44363/api/Department',{
method:'POST',
headers:{
'Accept':'application/json',
'Content-Type':'application/json'
},
body: JSON.stringify({
DepartmentID:null,
DepartmentName: event.target.DepartmentName.value
})
.then(res=> res.json())
.then((result)=>
{
alert(result);
},
(error)=>{
alert('Failed')
}
)
}
)
}
以下是我在屏幕上呈现的部分(可能不需要):
render(){
返回(
添加部门
部门名称
添加部门
接近
);
}}
导出默认的adddepmodel;
我不明白错误是什么?您应该像这样使用fetch
fetch('https://localhost:44363/api/Department', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
DepartmentID: null,
DepartmentName: event.target.DepartmentName.value
}),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
我想你少了一个
)
。
在您的示例中,您正在调用
then(…)
函数,返回值为JSON.stringify(…)
Hi我在哪里缺少a)您能告诉我我在哪里缺少a)是的,得到了error@sandeep普拉丹成功了吗?
fetch('https://localhost:44363/api/Department', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
DepartmentID: null,
DepartmentName: event.target.DepartmentName.value
}),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});