0

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.

2
  • yes, there's a better way. Creating extra deferred objects is a common jQuery promise anti-pattern. Commented Apr 16, 2015 at 15:34
  • Note: in the 2nd snippet, getSomeData isn't being invoked. .done() would be used as a method of the function itself, rather than its return value. Is this a typo? Commented Apr 16, 2015 at 15:35

2 Answers 2

1

If you want to pass the json dataType field, but want to use Promises instead of the anonymous callback, just pass null for the callback.

From the docs:

A callback function that is executed if the request succeeds. Required if dataType is provided, but can be null in that case

Hence all you need is:

function getSomeData(phpFile, postData) {
    return $.post(phpFile, postData, null, 'json');
}

Of course if your server is responding with the appropriate Content-type: field then JQuery will happily just guess at the data type anyway.

Sign up to request clarification or add additional context in comments.

Comments

1

If you are just looking to wrap up the parameters and return an object you can chain into, wrap up a standard ajax call

function post(phpFile, postData){
    return $.ajax({
      type: "POST",
      url: phpFile,
      data: postData,
      dataType: 'json'
    });
}

post('myPhpFile.php', postData,).done(function(function(data){
   console.log(data.status)
});

6 Comments

Duh... Getting so used to the convenience functions, forgot about the originals.
@tdoakiiii $.post will still do what you want perfectly happily, though.
@Alnitak Yes, using a null callback will work. I just don't like the way it looks.
@Tyrsius well, that's just being needlessly fussy, IMHO ;-)
@Alnitak I suppose; though "making the code look nice" is half of what this site is about =)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.