【问题标题】:How to get data from axios response [duplicate]如何从axios响应中获取数据[重复]
【发布时间】:2019-07-09 18:54:50
【问题描述】:

尝试使用 axios 发出 http 请求,然后从请求中访问某些属性。属性未定义,尽管它存在

public getApiURL() {
    axios.get('https://xxxxxxxxx.com/metadata.json')
        .then(res => {
            console.log(res.data); // {"apiUrl":"https://xxxx.com/Api"}
            console.log(res.data.apiUrl); // undefined
        }).catch(err => {
            console.log('error', err);
        })
}

【问题讨论】:

  • 也许你必须做一个 JSON.parse(res.data).apiUrl
  • console.log(typeof res.data)

标签: javascript node.js axios


【解决方案1】:

试试这个

`

public getApiURL=async ()=> {
    try{
       let result= await axios.get('https://xxxxxxxxx.com/metadata.json')
       const data = JSON.parse(result.data);
       console.log(data.apiUrl);
       }catch(err){
           console.log(err);
       }    
}

`

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 2020-03-22
    • 2020-08-10
    • 2020-12-13
    • 2019-11-26
    • 2023-03-25
    • 2019-12-02
    • 2020-09-01
    相关资源
    最近更新 更多