gpt4 book ai didi

javascript - React + Flux, ES6, Babel ActionCreate using json-server and super agent, 数据不响应

转载 作者:行者123 更新时间:2023-12-04 00:06:14 27 4
gpt4 key购买 nike

您好,我正在尝试使用 json-server 模拟我正在构建的 React Flux ES6 应用程序的 API。但是当我使用 super 代理节点模块从 Action 创建者发出请求时,回调中的数据是未定义的

这是我的代码

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 在没有 super 代理节点模块的情况下发出这个 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));

谢谢!

关于javascript - React + Flux, ES6, Babel ActionCreate using json-server and super agent, 数据不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31554290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com