Melova1985
Melova1985

Reputation: 407

$.post versus $.ajax

I recently asked about how to post from a form with MVC. Thank you everyone for your help and advice.

I noticed some advice talking about using $.post and other people talk using $.ajax

Is there any difference and which is the best to use when I use Microsoft MVC version 3.

Please just reply with an answer for MVC.

Thank you very much.

Upvotes: 1

Views: 152

Answers (3)

ThiefMaster
ThiefMaster

Reputation: 318508

$.post calls $.ajax internally. However, I prefer using $.ajax since it looks better with proper indentation etc:

$.post('someURL', {
    my: 'data',
    more: 'data'
}, function(resp) {
    /* ... */
});

vs.

$.ajax({
    type: 'POST',
    url: 'someURL',
    dataType: '...',
    data: {
        my: 'data',
        more: 'data'
    },
    success: function(resp) {
        /* ... */
    }
});

The later is twice as long but much more readable IMO.

Upvotes: 4

Clement Herreman
Clement Herreman

Reputation: 10536

Both are the same. $.post is just a shorthand to $.ajax.

This is a shorthand Ajax function, which is equivalent to:

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

Upvotes: 0

Infeligo
Infeligo

Reputation: 11802

jQuery.post() is a shorthand Ajax function, which is equivalent to:

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

Upvotes: 1

Related Questions