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 });
精彩评论