jquery 多个Ajax请求

发布于 2024-12-05 06:51:41 字数 2116 浏览 1 评论 0原文

此代码段允许将多个文件拖动到框中。 FileReader会创建每个文件的斑点,然后使用AJAX RQ:

$.each( e.dataTransfer.files, function(index, file){
    var fileReader = new FileReader();

    //..generate BLOB from file 


    fileReader.onloadend = (function(file) {                                
        return function(e) {                            
            uploadfilelist.append('<li>' + file.fileName + '</li>');
            //send every single file to server
            var destfoldername=CURRENTPATH;
            var uploadfilename=file.fileName;

            var fd = new FormData();
            fd.append("param1", destfoldername);
            fd.append("param2", uploadfilename);
            fd.append("param3", blob);

            $.ajax({
                type:"POST",
                url:"url",
                data:fd,
                contentType: false,
                processData: false,             
                beforeSend:function (xhr){ 
                    //custom headers
                },  
                success: function(data, textStatus, jqXHR){

                },
                complete: function(jqXHR){    
                    alert("State after complete: " + jqXHR.statusText);                 
                }           
            });
        };
    })(file);
    fileReader.readAsBinaryString(file);
}

QUASSAL:问题: 当没有处理前一个斑点时,服务器内部崩溃时会崩溃。

我找到了另一篇讨论这一点的文章:如何使所有ajax调用sequential? 使用async:false不是一个选项的“顺序”请求,它将阻止许多其他内容。

解决方案:??? 致电ajax forfile1,呼叫完成后,请致电file2,...致电ajax for file-n,

我真的很想使用jq deferred( http://api.jquery.com/category/deferred-object/ ) 例如,如下所述:

$.when($.ajax(???how to refer to THIS ajax call).done(function(){
//call this ajax for the next file again or use $.ajax(code).then()?
});  

我真的很抱歉,但我不知道该如何正确。

感谢您的建议! h。

This code snippet allows to drop multiple files dragged into a box. The filereader creates a blob of each file and then EACH file should be send to server using the ajax rq:

$.each( e.dataTransfer.files, function(index, file){
    var fileReader = new FileReader();

    //..generate BLOB from file 


    fileReader.onloadend = (function(file) {                                
        return function(e) {                            
            uploadfilelist.append('<li>' + file.fileName + '</li>');
            //send every single file to server
            var destfoldername=CURRENTPATH;
            var uploadfilename=file.fileName;

            var fd = new FormData();
            fd.append("param1", destfoldername);
            fd.append("param2", uploadfilename);
            fd.append("param3", blob);

            $.ajax({
                type:"POST",
                url:"url",
                data:fd,
                contentType: false,
                processData: false,             
                beforeSend:function (xhr){ 
                    //custom headers
                },  
                success: function(data, textStatus, jqXHR){

                },
                complete: function(jqXHR){    
                    alert("State after complete: " + jqXHR.statusText);                 
                }           
            });
        };
    })(file);
    fileReader.readAsBinaryString(file);
}

PROBLEM:
the server internal crashes when receiving a next blob while not having processed the former one.

I found another Post discussing this: How to make all AJAX calls sequential?
A "sequential" Request using async:false is not an option, it would block a whole lot of other things..

SOLUTION: ???
Call ajax forfile1, when call is done, call ajax for file2, ...call ajax for file-n

I would really like to use JQ Deferred (http://api.jquery.com/category/deferred-object/)
e.g. as described here: http://api.jquery.com/jQuery.when/

$.when($.ajax(???how to refer to THIS ajax call).done(function(){
//call this ajax for the next file again or use $.ajax(code).then()?
});  

I am really sorry, but I do not know how to get it right.

Thanks for any suggestions!
h.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听不够的曲调 2024-12-12 06:51:41

那么有一个插件队列插件:

http://plugins.jquery.com/project/ajaxqueue

由 John Resig 本人撰写。

Well there is a plugin the queue plugin:

http://plugins.jquery.com/project/ajaxqueue

It's written by John Resig himself.

甜中书 2024-12-12 06:51:41

当然,您可以通过使用下一个承诺来解决,从而在没有插件的情况下对它们进行排队,

$.ajax(/*options*/)
.then(function(res) {
  // do something with result
  // call 2
  return $.ajax(/* options */);
})
.then(function(res) {
  // call 3
  return $.ajax(/* options */);
})  // and so on
.fail(function(err) {
  // catch any error from any ajax call
});

这几乎与嵌套回调相同,并且具有可读性和捕获所有错误的额外好处

you can certainly queue them without a plugin by resolving with the next promise

$.ajax(/*options*/)
.then(function(res) {
  // do something with result
  // call 2
  return $.ajax(/* options */);
})
.then(function(res) {
  // call 3
  return $.ajax(/* options */);
})  // and so on
.fail(function(err) {
  // catch any error from any ajax call
});

this is almost the same thing as nesting the callbacks with the added benefit of readability and the catch all for errors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文