我正在尝试填充一个数组,该数组将以JSON格式在POST请求中发送。问题在于,当它发送到后端时,它会收到JSONArray null。
我的构造函数是这样的:
constructor(props) {
super(props);
this.state = {
name: '',
text: '',
date: '',
treatment: [],
treatments: [],
app: '',
appointments: [],
dropdownOpen: false,
dropdownOpen2: false
}
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
fetch('http://localhost:8080/annotations', {
method: 'POST',
headers: {'Content-Type':"application/json; charset=utf-8"},
body: JSON.stringify({
name: this.state.name,
text: this.state.text,
date: this.state.date,
treatment: [{
name: this.state.treatment.name,
type: this.state.treatment.type,
price: this.state.treatment.type,
create_date: this.state.treatment.create_date,
ann: this.state.treatment.ann
}],
app: this.state.app
})
}).then(response => response.json())
.then(function(myJson) {
console.log(JSON.stringify(myJson));
})
console.log(this.state.treatment)
//.then(data => this.setState({ name: data.name, text: data.text, date: data.text, treatment: data.treatment, app: data.app }))
}
console.log(this.state.treatment)
显示正确填充了数组处理,但是console.log(JSON.stringify(myJson))
将treatments
显示为null
。如何填充格式正确的JSONArray?
谢谢!