Difference between $.post and $.ajax?

JavascriptJqueryAjaxasp.net MvcSerialization

Javascript Problem Overview


Curious if anyone knows what the difference is in regards to the data parameter.

I have a $.post method that takes a $('#myform').serialize() as my data param and works.

If I try the same using the $.ajax() approach, it doesn't work as my data param doesn't appear correct.

Does anyone know the difference and what I might use instead of the above .serialize?

Javascript Solutions


Solution 1 - Javascript

This jquery forum thread sums it up:

> $.post is a shorthand way of using $.ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code. $.get works on a similar principle. > > —addyosmani

In short, this:

$.post( "/ajax", {"data" : json }) 

Is equivalent to the following:

$.ajax({ 
  type: "POST", 
  url: "/ajax", 
  data: {"data": json} 
});

Solution 2 - Javascript

The problem here is not the fact $.ajax() is not working, it is because you did not set the type parameter in the Ajax request and it defaults to a GET request. The data is sent via the query string for get and if your backend expects them as post parameters, it will not read them.

$.post is just a call with $.ajax(), just with the type set. Read the docs and you will see that $.ajax() defaults to a GET as I mentioned above.

If you go to the jQuery.post page in the jQuery docs it shows you the $.ajax request with the type set. Again read the docs.

Solution 3 - Javascript

After re-reading some online documentation, I decided to stick with $.post over $.ajax.

The $.ajax method's data param does something different than the $.post method does, not sure what exactly, but there is a difference.

The only reason I wanted to use $.ajax is because I wanted to be able to handle events and didn't realize I could do so with $.post.

Here is what I ended up with

function GetSearchItems() {
    var url = '@Url.Action("GetShopSearchResults", "Shop", New With {.area = "Shop"})';
    var data = $("#ShopPane").serialize();
    // Clear container
    $('#shopResultsContainer').html('');
    // Retrieve data from action method
    var jqxhr = $.post(url, data);
    // Handle results
    jqxhr.success(function(result) {
        //alert("ajax success");
        $('#shopResultsContainer').html(result.ViewMarkup);
    });
    jqxhr.error(function() {
        //alert("ajax error");
    });
    jqxhr.complete(function() {
        //alert("ajax complete");
    });
    
    // Show results container
    $("#shopResultsContainer").slideDown('slow');
}

JQuery 3.x

> 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.

var jqxhr = $.post(url, data);
// Handle results
jqxhr.done(function(result) {
    //alert("ajax success");
});
jqxhr.fail(function() {
    //alert("ajax error");
});
jqxhr.always(function() {
    //alert("ajax complete");
});

https://api.jquery.com/jquery.post/

Solution 4 - Javascript

Are you specifying this as the data parameter. $.post is just a shorthand for $.ajax which is expecting the following.

$.ajax({
    type : 'POST',
    url : url,
    data : data,
    success : success,
    dataType : dataType
});

Solution 5 - Javascript

Just as a complementary, in the accepted answer, it is mentionned that "The $.ajax method's data param does something different than the $.post method does, not sure what exactly, but there is a difference"

please try using :

    { 
        ... 
        data: JSON.stringify(yourJsonData), 
        ... 
    }

Else the json object get's inserted in the payload as a url-encoded string.

Solution 6 - Javascript

Using $.ajax we can make GET or POST requests. Using $.post we can make only post request. Using $.get we can make only get request.

$.ajax()	// Performs an async AJAX request
$.get()	    // Loads data from a server using an AJAX HTTP GET request
$.post()	// Loads data from a server using an AJAX HTTP POST request

Solution 7 - Javascript

In $.ajax you are able to synchronize, but it is not possible in the $.post function. To synchronize means that you can get the returned result.

var tmp;
$.ajax({
    'async': false,
    'type': "POST",
    'global': false,
    'dataType': 'html',
    'url': "Your Url",
    'data': {'type': 'data'},
    'success': function (data) {
        tmp = data;
    }
});
alert(tmp);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEd DeGagneView Question on Stackoverflow
Solution 1 - JavascriptrahulView Answer on Stackoverflow
Solution 2 - JavascriptepascarelloView Answer on Stackoverflow
Solution 3 - JavascriptEd DeGagneView Answer on Stackoverflow
Solution 4 - JavascriptdoveView Answer on Stackoverflow
Solution 5 - JavascriptspiloteView Answer on Stackoverflow
Solution 6 - Javascriptkarthik kasubhaView Answer on Stackoverflow
Solution 7 - JavascriptnagakingView Answer on Stackoverflow