获取ReactJS中JSON验证列表的错误
在我的api测试中,它可以如下图所示运行。但是,当我尝试在React.JS中编写代码时,无法传入用户Id。这有什么问题 但当我尝试在前端编写代码时,用户Id无法传递到api。 考虑下面的代码:获取ReactJS中JSON验证列表的错误,json,reactjs,api,response,Json,Reactjs,Api,Response,在我的api测试中,它可以如下图所示运行。但是,当我尝试在React.JS中编写代码时,无法传入用户Id。这有什么问题 但当我尝试在前端编写代码时,用户Id无法传递到api。 考虑下面的代码: constructor (props){ super(props); const Users_id = localStorage.getItem('id'); this.state ={ users_id: Users_id, event_name:
constructor (props){
super(props);
const Users_id = localStorage.getItem('id');
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
this.create = this.create.bind(this);
this.onChange = this.onChange.bind(this);
}
create(){
// const loginEmail = localStorage.getItem('loginEmail');
const Users_id = localStorage.getItem('id');
this.setState({Users_id})
// console.log(loginEmail)
PostData('api/event/submit', this.state).then ((result) => {
let responseJSON = result;
console.log(responseJSON);
console.log(this.state)
});
}
console.log(responseJSON)中显示的错误:
//更新的PostData
return new Promise((resolve, reject) => {
fetch( BaseUrl+type, {
method: "POST",
body: JSON.stringify(userData),
Accept: 'application/json',
// mode: 'no-cors',
headers:
{
'Content-Type': 'application/json'
},
})
.then((response) => response.json())
.then((responseJson) => {
resolve(responseJson);
})
.catch((error)=>{
console.error('Error:', error);
})
});
Storage.getItem返回一个字符串。该id应转换为一个数字 const Users_id=parseInt(localStorage.getItem('id')代码>
在发送之前,您可能需要检查
isNaN(用户id)
。或者让它失败,然后处理错误。Storage.getItem返回一个字符串。该id应转换为一个数字
const Users_id=parseInt(localStorage.getItem('id')代码>
在发送之前,您可能需要检查
isNaN(用户id)
。或者让它失败,然后处理错误。在将用户id插入props之前,您需要检查本地存储中是否存在该用户id
super(props);
const Users_id = localStorage.getItem('id') || '';
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
当您想要设置状态时,您需要传递在状态中使用的相同名称
const Users_id = localStorage.getItem('id');
if(Users_id){
this.setState({users_id: Users_id})
}
在将用户id插入props之前,需要检查本地存储中是否存在该用户id
super(props);
const Users_id = localStorage.getItem('id') || '';
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
当您想要设置状态时,您需要传递在状态中使用的相同名称
const Users_id = localStorage.getItem('id');
if(Users_id){
this.setState({users_id: Users_id})
}
API是否希望用户id为数字?还是字符串化的数字?@Jacob ya,users\u id是数字。我相信getItem会返回字符串。在更新状态之前,尝试放置
console.log(用户id的类型)
。您能显示PostData方法的代码吗?@Jacob您说得对,控制台日志的响应是字符串。那么,有没有办法将字符串转换为数字?API是否希望用户id为数字?还是字符串化的数字?@Jacob ya,users\u id是数字。我相信getItem会返回字符串。在更新状态之前,尝试放置console.log(用户id的类型)
。您能显示PostData方法的代码吗?@Jacob您说得对,控制台日志的响应是字符串。那么,有没有办法把字符串转换成数字呢?