React + Flux, ES6, Babel ActionCreate使用json-server和超级代理,数据不响

React + Flux, ES6, Babel ActionCreate using json-server and super agent, data not in response

本文关键字:代理 数据 使用 Flux ES6 Babel React ActionCreate json-server      更新时间:2023-09-26

你好,我正在尝试使用json-server来模拟我正在构建的React Flux ES6应用程序的api。但当我使用超级代理节点模块向操作创建者发出请求时回调中的数据是undefined

这是我的代码

import Dispatcher from '../Dispatcher';
import Constants from '../Constants';
import request from 'superagent';
export default {
   setQuestions(guides) {
        Dispatcher.handleViewAction({
            type: Constants.ActionTypes.SET_QUESTIONS,
            data: guides
        });
   },
   getQuestionsFromServer() {
      let self = this;
      let destination = 'http://localhost:3000/questionnaires';
      // request from json service.
      request
        .get(destination)
        .set({
              'X-Requested-With': 'XMLHttpRequest'
          })
        .end(function(response) {
          // response is empty. why???
          if (response.ok) {
              let guideData;
              guideData = response.body;
              self.setQuestions(guideData);
          }
      });
   }
};

我的网络选项卡说请求发生了,但我无法访问回调中的响应。

我弄清楚了如何通过使用fetch es2015使这个xhr请求没有超级代理节点模块。在这里看到的:https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch

getQuestionsFromServer() {
    let self = this;
    let destination = 'http://localhost:3000/questionnaires';
  // request from json service.response.json()
  fetch(destination)
        .then(response => response.json())
    .then(data => {
          this.setQuestions(data[0].questions);
      })
      .catch(e => console.log("Error", e));

}

谢谢!