Process data from return by an ajax request, but outside the callback function
I have this code.
/*
Get the source of the data.
*/
if (if_the_source_is_an_url) {
$.getJSON(url_here, function(returnedData){ theData = returnedData; });
}
/*
Here we have the default processing of theData.
*/
So i need that if the user provides an url, the retured data will be processed but outside of the callback function, becouse i need to reuse the data processing code. So, i came with this ideas.
- Use the break command
$.getJSON(url_here, function(returnedData){ theData = returnedData; break;});
- Change to an $.ajax request but with async: false
just put the data processing inside a function and call it from the
callback function.if (url) $.getJSON(url_here, function(returnedData){ customFunction(returnedData); }); else customFunction(defaultData);
Any ideas if this will work (i did not teste开发者_运维技巧d it becouse im not at home), or are there better practices for this?
Attach handlers/functions using jQuery.ajaxComplete to filter/transform/etc your data. The handles your attach here will be called for all of your ajax requests
精彩评论