gpt4 book ai didi

javascript - react native api 调用响应

转载 作者:行者123 更新时间:2023-11-30 20:35:27 25 4
gpt4 key购买 nike

我正在发出一个 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{} 而只有 {}

最佳答案

尝试在你的 JS 控制台中输入:

var myObject = {a: 1, b: 2}

然后检查你的新对象:

我的对象

它应该显示为:

对象 { a: 1, b: 2 }

{}Object{} 是同一事物。后者是控制台选择显示 JavaScript 对象的方式,因为它们具有与其关联的其他数据,如果您单击以展开对象,您可以看到这些数据:

{...}
一个:1
乙:2
__proto__: 对象 {...}

查看本教程以获取 working with objects

关于javascript - react native api 调用响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49902675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com