简体   繁体   English

在ajax beforeSend之前先解析一个函数

[英]Resolve a function first before ajax beforeSend

I was trying to check the file first before I upload it using 'fileuploader', now on my beforeSend function: 我试图先检查文件,然后再使用'fileuploader'上传文件,现在在我的beforeSend函数上:

beforeSend: function(item, listEl, parentEl, newInputEl, inputEl) {
            var file = item.file;

            let readfile = function(file){
                return new Promise(function(resolve, reject){
                console.log(file);
                    var reader = new FileReader();
                    var duration = 0;
                    reader.onload = function() {
                        var aud = new Audio(reader.result);
                        aud.onloadedmetadata = function(){
                            resolve(aud.duration);
                        }; 
                    };
                reader.readAsDataURL(file); 
                });
            }

          return readfile(file).then(function(res){ 
                if(res>60){ 
                    console.log('more than 60');
                    return false; 
                 }
                 else{
                    console.log('uploaded');
                    return true;
                 }

        }); 

        },

My readfile function actually waits for the promises to finish, but the beforeSend function is not pausing before the readfile function is done. 我的readfile函数实际上在等待诺言完成,但是beforeSend函数在readfile函数完成之前不会暂停。

How do I do this please? 我该怎么做?

I know that its not returning false since an http request is still made even if I return false on my promises. 我知道它不会返回false,因为即使我在诺言中返回false,仍然会发出http请求。

You can do this. 你可以这样做。

$(document).on('sendAjax',function(){
//Write your ajax code here
});

$(document).trigger("sendAjax");

Create custom event sendAjax, bind it with document and trigger the event after file is uploaded succesfully. 创建自定义事件sendAjax,将其与文档绑定,并在文件成功上传后触发事件。 Take file upload function in $(document).ready() $(document).ready()获取文件上传功能

$(document).ready(function(){
 function myfunction(){
  var file = item.file;

            let readfile = function(file){
                return new Promise(function(resolve, reject){
                console.log(file);
                    var reader = new FileReader();
                    var duration = 0;
                    reader.onload = function() {
                        var aud = new Audio(reader.result);
                        aud.onloadedmetadata = function(){
                            resolve(aud.duration);
                        }; 
                    };
                reader.readAsDataURL(file); 
                });
            }

          return readfile(file).then(function(res){ 
                if(res>60){ 
                    console.log('more than 60');
                    return false; 
                 }
                 else{
                    console.log('uploaded');
                    $(document).trigger("sendAjax"); 
                    return true;
                 }

        }); 
 }
});

By this way ajax will be only send after file uploaded succesfully. 这样,只有在文件成功上传后,才发送ajax。

Hope it helps. 希望能帮助到你。

As per the jQuery code, beforeSend() does not use the returned value from the callback function other than to check if false was returned 根据jQuery代码, beforeSend()除了检查是否返回false之外,不使用回调函数返回的值

https://github.com/jquery/jquery/blob/d7237896c79a5a10d85fcdec199c5657a469a92b/src/ajax.js#L652 https://github.com/jquery/jquery/blob/d7237896c79a5a10d85fcdec199c5657a469a92b/src/ajax.js#L652

 // Allow custom headers/mimetypes and early abort if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || completed ) ) { // Abort if not done already and return return jqXHR.abort(); } 

So not only is returning any value other than false not used, by the time your promise resolves the beforeSend callback will have already finished and the ajax request already started/finished. 因此,不仅不会使用false以外的任何值,而且在您的诺言解析到beforeSend回调的时候,ajax请求也已经开始/完成了。

If you want to do some async operation beforehand you will need to do it before the actual jQuery.ajax() call 如果您想事先进行一些异步操作,则需要在实际的jQuery.ajax()调用之前进行

Example: 例:

new Promise((resolve,reject)=>{
   //...
}).then(()=>{
   return $.ajax({
     //....
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM