English 中文(简体)
杂质 Ajax 请求
原标题:jquery multiple Ajax request

该法典允许将多份被拖入箱子的档案。 存档器制作每个档案的书目,然后使用Ajax rq向服务器发送EACH文件:

$.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()?
});  

我确实感到担忧,但我不知道如何正确。

Thanks for any suggestions! h.

问题回答

你们当然可以通过兑现下一个诺言,不给他们带来gin。

$.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
});

这几乎与把呼吁与可读性带来的附加利益和所有错误的追索结合起来。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签