Jquery Cannot read the 'done' property from undefined - avoid this

I have a function that returns results (or not). The problem is that it does not return any value that I receive in the console, a message

cannot read the 'done' property from undefined

This is true, and I understand the problem. Also, this error will not make my code stop working, but I would like to know if there is a chance to avoid this?

Function in ajax:

function getDelivery(){ var items = new Array(); $("#tab-delivery tr").each(function(){ items.push({"id" : $(this).find('.form-control').attr('id'), "id_option" : $(this).find('.form-control').val()}); }); if(items.length > 0){ return $.ajax({ url: 'response.php?type=getDelivery', type: 'POST', data: {content: items} }); } } 

And to use it, I use:

 getDelivery().done(function(data){ // the problem is here if(data == false){ return; } }); 

So, is there a way to avoid the error? I tried without success the following:

 if(items.length > 0){ return $.ajax({ url: 'response.php?type=getDelivery', type: 'POST', data: {content: items} }); }else{ return false; } 

And I get the error message:

Uncaught TypeError: undefined is not a function

+6
source share
1 answer

You can simply return the pending, so the done() callback will not generate errors, and you can decide whether to solve it or not

 if(items.length > 0){ return $.ajax({ url: 'response.php?type=getDelivery', type: 'POST', data: {content: items} }); }else{ var def = new $.Deferred(); def.resolve(false); return def; } 

Fiddle

+7
source

All Articles