【发布时间】:2018-04-18 14:43:43
【问题描述】:
我正在发出一个 http 调用请求,但由于某种原因,响应有点奇怪!
我的电话看起来像:
getOne: id => {
return axios.get('posts/1', {
params: {
id: id
}
}).then(function (response) {
return response;
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
return error;
} else if (error.request) {
// The request was made but no response was received
return error.request;
} else {
// Something happened in setting up the request that triggered an Error
return error.message;
}
});
},
从组件中我称之为
categoryApi.getOne(1).then(response => {
if (response.data) {
this.setState({isLoading: false});
console.log(response.data);
this.setState({categories: this.state.categories.push(response.data)});
console.log(this.state.categories);
} else {
this.setState({isLoading: false});
Alert.alert(
'Something wrong happened!',
'My Alert Msg',
[],
{cancelable: true}
)
}
});
在控制台中,响应如下所示:
Object {
"body": "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto",
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"userId": 1,
}
问题是我希望响应是像这样的对象:
{
"body": "quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto",
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"userId": 1,
}
我需要更改什么以使我没有 Object{} 而只有 {}
【问题讨论】:
标签: javascript json react-native