响应本机 API 调用响应



我正在发出一个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,
         }

我需要更改什么,这样我才没有对象{},只有{}

尝试在 JS 控制台中键入以下内容:

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

然后检查新对象:

myObject

它应显示为:

Object { a: 1, b: 2 }

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

{...} a: 1 b: 2 __proto__: Object {...}

查看本教程,了解如何使用对象

最新更新