开发者

$.post versus $.ajax

开发者 https://www.devze.com 2023-02-28 15:01 出处:网络
I recently asked about how to post from a form with MVC. Thank you everyone for your help and advice.

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开发者_StackOverflow 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.


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


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

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


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
});
0

精彩评论

暂无评论...
验证码 换一张
取 消