简体   繁体   English

如何通过Vue中的axios获取json对象?

[英]How to fetch json object through axios in vue?

I'm trying to fetch an object from JSON generated on wordpress endpoint. 我正在尝试从在wordpress端点上生成的JSON获取对象。 My initial goal is to display images from custom fields in wordpress on the vue app I'm building for a client. 我的最初目标是在我正在为客户端构建的vue应用上的wordpress中显示来自自定义字段的图像。

Right now I'm just trying to test to see if I can fetch specific object, and then I will continue to fetch a specific image url. 现在,我只是在尝试测试是否可以获取特定的对象,然后继续获取特定的图像URL。

How can I fetch the "acf" object in json? 如何获取json中的“ acf”对象?

Should i instead filter the data to only show the object of interest, what would be the best practice to continue with here? 我是否应该过滤数据以仅显示感兴趣的对象,所以继续此处的最佳实践是什么?

The code I've written works, and it fetches the json. 我编写的代码有效,并且可以获取json。 However, I'm trying to only fetch the specific object "acf" in: https://backend.rogalandtt.no/wp-json/markers/v1/post . 但是,我试图仅在以下位置获取特定对象“ acf”: https : //backend.rogalandtt.no/wp-json/markers/v1/post

by changing the response to "res.data" it fetches it. 通过将响应更改为“ res.data”,它将获取它。 The way I understand it would be to add "res.data.acf" to fetch the object, but it returns undefined. 据我了解,这将是添加“ res.data.acf”来获取对象,但它返回的是未定义的。

    <template>
      <div id="app">
        {{ images }}
      </div>
    </template>
    <script>
    import Vue from 'vue'
    import axios from 'axios'
    import VueAxios from 'vue-axios'

    export default {
      data: () => ({
        markers: null,
        id: null,
        images: []
      }),
      async mounted () {
        axios
          .get('https://backend.rogalandtt.no/wp-json/markers/v1/post')
          .then(res => {
            (this.images = res.data /* Adding .acf shows undefined */)
            console.log(this.images)
          })
      }
    }
    </script>

In console log it just shows up as undefined. 在控制台日志中,它只是显示为未定义。

I figured this out by using stringify and type of. 我通过使用stringify和type弄清了这一点。 Stringify such as JSON.stringify(MyResponseValue.data.someOtherValue) , or using type of such as response.data[0].MyOtherValue to define a specific object I am trying to fetch from a Json schema. JSON.stringify(MyResponseValue.data.someOtherValue) ,例如JSON.stringify(MyResponseValue.data.someOtherValue) ,或使用诸如response.data[0].MyOtherValue类的类型来定义我尝试从Json模式中获取的特定对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM