Question regarding to $.ajax() with type "POST" vs $.post()

Kinderly Wade
Kinderly Wade used Ask the Experts™
on
Hi Experts

I came across this scenario which I can do a post with JQuery ajax() or post(). I am not sure which one to use. I read some information from web but I believe the information wasn't correct because the website mentioned that $.post() only has success callback but I believe it has other function from this link: https://api.jquery.com/jquery.post/ which mentioned that "The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead."

Now I am kind of confused as to which one to use. I will provide some feedback of what I am trying to do perhaps it will be easier to find the correct method for me. I have a login page that will take inputs from users (userID and password). I am trying to post the information to another page which it will validate user's input. If information is correct then it will return something  along the line of success() callback.

Rest of my pages will follow the same post structure. I need some suggestions as to which post should I use. Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Commented:
I suggest you to use $.post. As you described your intensions are specific without other feature like errors cache etc..
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
You can use either $.post is just a short hand for $.ajax. If you are going to be doing standard POST the $.post is fine - if you need to manipulate headers and disable / enable fringe functionality then you use $.ajax.

Personally I always use $.ajax mainly out of habit but also because it gives consistency to my code.
As for the success done  fail etc. as of jQuery 1.5 all jQuery Ajax methods support the promise interface (if you want to know more about Promises you can read about them here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)

 .success, .error and .complete are removed as of jQuery 3.0 so you should rather use the .done(), .fail() and .always() callbacks.

You can read more about this here http://api.jquery.com/jQuery.post/#jqxhr-object
Kinderly Wadeprogrammer

Author

Commented:
Thanks all for the feedback. I will keep them all under $ajax
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial