javascript - 未捕获( promise )TypeError : response. json 不是函数

标签 javascript vue.js axios

我正在尝试从我的 firebase 后端加载数据但收到此错误

Uncaught (in promise) TypeError: response.json is not a function

我的代码如下:

import axios from 'axios';
export const loadData = ({ commit }) => {
  console.log('getting data from server...');

  axios
    .get('data.json')
    .then(response => response.json())
    .then(data => {
      if (data) {
      
        const stocks = data.stocks;
        const stockPortfolio = data.stockPortfolio;
        const funds = data.funds;

        const portfolio = {
          stockPortfolio,
          funds
        };
        commit('SET_STOCKS', stocks);
        commit('SET_PORTFOLIO', portfolio);
        console.log('Commit done ');
      }
    });
};

但是,如果我尝试使用 response.data 而不是 response.json 它会工作并成功加载数据,所以我很好奇有什么区别以及为什么第一个一个不起作用。

最佳答案

由于 axios 包,它有一个特定的响应模式 https://github.com/axios/axios#response-schema

The response for a request contains the following information.

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  statusText: 'OK',

  // `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance in the browser
  request: {}
}

关于javascript - 未捕获( promise )TypeError : response. json 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453454/

相关文章:

javascript - 如何使用 A-Frame 设置对象的旋转以匹配触发时的 Vive Controller 旋转?

javascript - 使用 Underscore.js 使用 'contains' 过滤属性上的对象数组

javascript - 在 Amcharts 中增加饼图的大小

javascript - 在 vue 应用程序中使用 javascript 重写 jQuery 禁用按钮

javascript - 使用 vue 数据构建 axios url - 未定义

google-app-engine - 将 base64 字符串数据发送到服务器但收到空表单值

javascript - 是否可以 POST 请求响应类型 "array buffer"?

javascript - AngularJS 中的工厂

javascript - 当我更新Vuex状态时,V-snackbar不显示

javascript - vue js html中根据json数据动态创建Form?提供 fiddle