获取分配给变量的 fetch 返回的值
Get value returned form fetch assigned to a variable
我想获得分配给数据变量
的returned值"responseData"
fetch('http://localhost:8080/users/update, {
method: 'PUT',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({
name:this.state.name,
Level:this.state.activity,
height:this.state.height,
weight:this.state.weight
})})
.then((response) => {
return response.json()
})
.then((responseData) => {
console.log(responseData)
this.setState({
data:responseData
})
}
);
我确实 return 是的,我只想将它分配给我已经尝试使用的变量
this.setState({
data:responseData
})
但是没用
@Asad 谢谢,虽然我试图将 json 值分配给字符串变量,但我发现这是我的错误,这是正确的代码
this.setState({
data:JSON.parse(responseData)
})
fetch('http://localhost:8080/users/update, {
method: 'PUT',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({
name:this.state.name,
Level:this.state.activity,
height:this.state.height,
weight:this.state.weight
})})
.then((response) => {
return response.json()
})
.then((responseData) => {
console.log(responseData)
this.setState({
data:JSON.parse(responseData)
})
}
);
我想获得分配给数据变量
的returned值"responseData"fetch('http://localhost:8080/users/update, {
method: 'PUT',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({
name:this.state.name,
Level:this.state.activity,
height:this.state.height,
weight:this.state.weight
})})
.then((response) => {
return response.json()
})
.then((responseData) => {
console.log(responseData)
this.setState({
data:responseData
})
}
);
我确实 return 是的,我只想将它分配给我已经尝试使用的变量
this.setState({
data:responseData
})
但是没用
@Asad 谢谢,虽然我试图将 json 值分配给字符串变量,但我发现这是我的错误,这是正确的代码
this.setState({
data:JSON.parse(responseData)
})
fetch('http://localhost:8080/users/update, {
method: 'PUT',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({
name:this.state.name,
Level:this.state.activity,
height:this.state.height,
weight:this.state.weight
})})
.then((response) => {
return response.json()
})
.then((responseData) => {
console.log(responseData)
this.setState({
data:JSON.parse(responseData)
})
}
);