I know that there is a shortcut to parse JSON with jQuery such as
$.post('myPhpFile.php', postData, function(data){
console.log(data.status) // "OK"
},'json');
But I really hate anon functions and I also really hate having to manually parse JSON each time. I was hoping I could do this (because I don't always do the same thing with the same data)
function getSomeData(phpFile, postData){
return $.post(phpFile, postData);
}
getSomeData.done(function(data){
console.log(data.status) // error, as is not JSON
},'json');
But in the .done() I have to $.parseJSON(data) first, and I have lots of these small functions that juts return $.posts.
Is there any other shortcut or nice functional way to still return the deferred object of $.post and have the data already parsed in json? I thought of
function getSomeData(phpFile, postData){
var defer = $.Deferred();
$.post(phpFile, postData, function(data){
defer.resolve(data);
},'json');
return defer.promise(data);
}
But I am OCD and now i have two deferreds. Just curious if there's a better way.
getSomeDataisn't being invoked..done()would be used as a method of the function itself, rather than its return value. Is this a typo?