我正在研究一个示例reactjs应用程序(在学习过程中).我有一个列出用户列表的页面和一个添加新用户的添加按钮.
当我单击添加按钮时,我应该导航到用户表单以创建新用户.
单击用户表单中的"提交"按钮后,它应导航回第一页,其中应列出用户列表以及新用户.
如何在反应中的页面之间导航?
我提交了下面提到的 reactjs 表单,
submit(){
if(this.checkRequiredField()){
$.ajax({
url: '/api/Accounts',
dataType: 'json',
type: 'POST',
data: {
Name: this.state.name,
StartDate :this.state.startDate,
EndDate : this.state.endDate,
UserCount: this.state.userCount
},
success: function(data, status, xhr) {
console.log('data added successfully');
}.bind(this),
error: function(xhr, status, err) {
console.error(status, err.toString());
}.bind(this)
})
}
Run Code Online (Sandbox Code Playgroud)
上面的ajax post将调用相应的Web Api post方法,数据成功插入到数据库中。
发布数据后,程序不会返回成功函数,而是调用错误函数并记录错误
parsererror SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列的数据意外结束
当我检查 xhr.status 时,该值为 201 并且 statustext 已创建。
为什么上面的ajax post调用返回解析器错误?如何解决这个问题?
在下面的数组中,我有带有键/值对的对象
var options = [{
key: "select",
value: null
}, {
key: "one",
value: "First Option"
},
{
key: "second",
value: "Second Option"
}];
Run Code Online (Sandbox Code Playgroud)
如何从选项数组中获取基于键的值?
例如,如果键是“select”,则应返回 null,如果键是“one”,则应返回“First Option”。