0

It seems, at the first glass, that we can use either of those. I'm wondering, however, when should we use one or the other, assuming a scenario where we can do the same thing with any of those two.

Thanks in advance, MEM

3 Answers 3

3

jQuery.post is a shorthand method which calls jQuery.ajax.

If you don't need any functionality not supported by the shorthand, you might as well use it.

It's defined like this:

post: function( url, data, callback, type ) {
    // shift arguments if data argument was omited
    if ( jQuery.isFunction( data ) ) {
        type = type || callback;
        callback = data;
        data = {};
    }

    return jQuery.ajax({
        type: "POST",
        url: url,
        data: data,
        success: callback,
        dataType: type
    });
},
Sign up to request clarification or add additional context in comments.

Comments

1

The $.post is actually derived from $.ajax. You can use $.post when you want want to set the POST method in your request. $.ajax allows you to set both GET and POST request methods.

1 Comment

Thanks a lot four the clarification. It would be nice to share some answers here... oh well... Thanks again.
0

Using $.ajax allows you to set callbacks for failure, and completion, while $.post has callback only for success. Also, $.ajax allows to set a callback to be invoked before the sending of the request and set other useful parameters as timeout time , username or password if the target script needs authentication.

In short, $.post is a useful shortcut to use in the simpler situation, while $.ajax offers full control on the request.

1 Comment

Thanks a lot, I'm sure the more I use jquery the more I can get about it. :)

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.