尝试通过Vue中的XMLHttpRequest将数据加载到对象中

时间:2018-11-01 22:02:01

标签: vue.js xmlhttprequest

我正在尝试从API端点获取一些不同的数据集。 是否可以通过我可以传递url以及要更新哪个数据数组的方式来重用此功能?

        data () {
            return {
              currencyData: [],
              modesData: [],
              regionData: [],
              ratingData: [],
              languageData: []
            };

          },


    methods: {
        fillLanguageData: function () {
              let xhr = new XMLHttpRequest();
              let self = this;
              xhr.open('GET', this.globalAPIBaseUrl + 'languages', true);
              xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
              xhr.onload = function () {
                self.languageData = JSON.parse(xhr.responseText);
                self.checkAllLoaded();
              };
              xhr.send();
            },
}

我曾希望这能行得通,但是我显然缺少了一些基本知识

dotest: function () {
      this.getAnyData('modes', this.modesData);
    },
    getAnyData: function (v1, v2) {
      let xhr = new XMLHttpRequest();
      let self = this;
      xhr.open('GET', this.globalAPIBaseUrl + v1, true);
      xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
      xhr.onload = function () {
        v2 = JSON.parse(xhr.responseText);
      };
      xhr.send();

    },

0 个答案:

没有答案