我正在使用reactjs的回流。从Store的回流 Action 中,我编写了一个 Action 来从asp.net web api获取数据,我认为这只是获取使用ajax的数据的唯一方法,有人跟我说,我可以使用插件jquery获取数据,但我不这样做不要相信它,因为 $.ajax 是最好的方法。我在谷歌上搜索了所有内容,但我看不到解决这个问题的方法。如果您知道需要解决的问题,请与我分享,我真的很感谢。
此外,我遇到了 ajax 全局变量和局部变量的问题。请查看我的代码,您可以看到从不返回值的粗体文本,问题仍然存在于成功 block 中,列表变量在 block 之外时不会更新。这是怎么回事?我该如何修复该错误?
再次非常感谢您!
(function (Reflux, WorkHistoryActions, global) {
global.workhistoryStore = Reflux.createStore({
listenables: [WorkHistoryActions],
init: function () {
this.storyArr = [];
},
getItems: function (resume_id) {
console.log(resume_id)
**var list = [];**
$.ajax({
type: "get",
url: global.getHost() + "/api/workhistories/6969607988340821009",
dataType: 'json',
crossDomain: true,
success: function (data) {
$.each(data, function (i, v) {
**list.push(v);**
})
}
});
**return list;**
},
})
最佳答案
我不知道如何在回流中做到这一点,但在正常的通量架构中你会这样做:
getItems: function (resume_id) {
$.ajax({
type: "get",
url: global.getHost() + "/api/workhistories/6969607988340821009",
dataType: 'json',
crossDomain: true,
success: function(result) {
workHistoryActionCreator.receiveItems(result);
},
error: function(error) {
workHistoryActionCreator.failToReceiveItems(error);
}
});
}
存储向调度程序注册 RECEIVED_WORK_HISTORY_ITEMS 和 FAILED_TO_RECEIVE_WORK_HISTORY_ITEMS 操作,设置其数据,然后触发更改事件。
关于reactjs - 在回流中,我如何从asp.net web api获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486463/