【问题标题】:How to get the json data use Axios in my Vue.js project如何在我的 Vue.js 项目中使用 Axios 获取 json 数据
【发布时间】:2018-09-08 01:05:40
【问题描述】:

我的项目的/src/static/js 目录中有一个server_config.json

{
  "ApiUrl":"http://localhost:8000"
}

在我的main.js,我想用axios来获取数据:

Vue.prototype.getConfigJson = function () {

  this.$http.get("/static/js/server_config.json").then((response)=> {

    Vue.prototype.ApiUrl = 'http://localhost:8000';
  }).catch((response)=> {

    if (response == null) {
      Vue.prototype.ApiUrl = 'http://localhost:8000';

    }
  });
}

在我的App.vue 中发出函数:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
  export default {
    mounted: function () {
      this.getConfigJson();
    }
  }
</script>

但我无法获取 json 数据,我会抓住这个:

catch((response)=> {}

responseundefined

【问题讨论】:

  • 您应该检查浏览器开发工具的网络面板以了解请求。

标签: json vue.js axios


【解决方案1】:

您可以通过以下方式在任何 Vue 组件或 .js 文件(main.js、store.js 等)中使用 axios:

import axios from 'axios';

axios.get('www').then(res => {// your code})

对于 Vue 文件,请确保您需要在标签内执行此操作:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
    import axios from 'axios';

    export default {
      mounted() {
        axios.get('www').then(res => {// your code})
      }
    }
</script>

【讨论】:

  • 'www' 是什么意思?
  • localhost:8000/static/js/server_config.json
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 2022-01-04
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 2019-07-14
  • 2019-04-11
相关资源
最近更新 更多