两个相同的ajax调用不同的参数,第二个覆盖第一个,导致结果不一样,每次刷新它。第一个setState我已经设置了测试:[],testsHistories:[]在第一个Ajax中,第二个测试集状态。为什么第二个Ajax调用会影响第一个?ReactJS两个相同的ajax调用不同的参数
class App extends Component {
constructor(props){
super(props);
this.onFormSubmit = this.onFormSubmit.bind(this);
this.state = { tests: [],
testsHistories: [], testInfo: []};
$.ajax({
url: "http://localhost:xxxx/api/Testing?sorton=datecompleted&order=asc",
success: (data) => {
console.log("success");
this.setState({tests: data, testsHistories: data}
);
},
error: function(xhr,status,err){
console.log('error');
}
});
$.ajax({
url: "http://localhost:xxxx/api/Testing?"+ admissionId,
// url: "http://localhost:xxxx/api/Testing?admissionId=5",
success: (data) => {
console.log("success");
this.setState({testInfo: data});
},
error: function(xhr,status,err){
console.log('error');
}
});
}
是否使用了赋值的'setState'?如果是这样,您将只能从一个查询中获得结果。要将结果“累加”为单个状态对象的属性,请使用[Object#assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) – Will
@DarrenSweeney为什么你会手动设置状态并放弃React自动处理状态更新? – sbking
@sbking你是对的,我读错了,我以为他是从 –