如何在FLUX中处理ajax响应
How to handle ajax response in FLUX
我是FLUX的新手,我有问题如何处理FLUX中的ajax。
我的情况如下:
我有文件commentAPI.js
//all js files are compiled from coffescript
// fetching all comments from server
_fetchComments: function() {
var promise;
promise = $.ajax({
url: "comments/show",
type: "GET",
dataType: "json"
});
return promise.then(function(response) {
// here should be any action ?
}, function(error) {
return console.log(error);
}); }
然后我有commentActions.js
fetchComments: function () {
allcomments=commentAPI._fetchComments();
return Dispatcher.dispatch({
actionType: ActionTypes.ALL_COMMENTS,
comments: allcomments
});
}
这段代码实际上不起作用,因为在commentActions.js中调用的函数_fetchComments返回整个承诺。
我想做的是:我想从ajax回调函数中获得响应,并将结果传递给我的有效载荷对象,然后通过Dispatcher在commentActions.js
中的_fetchComments()函数中调度它做这件事的最好方法是什么?我如何获得访问ajax回调函数响应?
你应该调度_fetchComments函数,当promise被解析后,调用fetchComments操作。
在react代码中你应该调用async函数(即_fetchComments)。
在你的例子中:
// fetching all comments from server
_fetchComments: function() {
var promise;
promise = $.ajax({
url: "comments/show",
type: "GET",
dataType: "json"
});
return promise.then(function(response) {
// put the sync action here
fetchComments(response)
}, function(error) {
return console.log(error);
}); }
不要忘记从动作中删除它(即fetchComments)
相关文章:
- 如何处理Ajax.BeginForm()OnError OnFailure回调
- Masony无法处理ajax响应
- 如何在phonegap应用程序中处理Ajax json响应
- 在嵌套循环中处理 AJAX (Angularjs)
- 运行 AJAX 需要什么 - 将 jQuery 添加到页面处理 AJAX 调用
- 在 Rails 中处理 ajax 请求
- 为什么不允许在jquery数据表服务器端处理ajax中使用成功
- JQuery无法处理ajax返回的内容
- 处理AJAX回调
- 难以处理ajax调用
- 额外的层可以更好地处理ajax调用
- 如何在响应时处理ajax错误
- DROPZONE - 在 fle 上传期间处理 AJAX 请求中的会话超时
- 在快递中.js有没有办法同时处理 ajax 表单提交和浏览器帖子提交
- 动态处理 ajax 结果
- 如何在没有jQuery的情况下处理Ajax浏览器兼容性
- CakePHP - 处理 ajax 调用中的 url
- 当服务器没有响应时如何处理 ajax 调用
- 全局和特定错误处理 AJAX jQuery
- 处理 AJAX 调用的 Web 工作人员 - 优化矫枉过正