I want to know difference between $.ajax and $.post in jquery. I search it but not getting clearification. Actually I am having drop down of states and cities and I am changing cities when states changes with jquery $.post. Here is my code of jquery
$.post('includes/change_cities.php', { 'state_id': state_id },
function( data ) {
var content = $( data );
$( "#cities" ).empty().append( content );
}
);
My Problem is when states changes for the first time it takes too much time to change cities about 4 to 5 seconds but after first time it becomes normal means changes cities without taking any time. So Is there any problem in my code or should I use any other method for this.
-Thanks
The performance Depends on your code and sql implementation.
$.post is subset of $.ajax in tersm of functionality.
$.ajax({
type: 'POST',
url: 'ajax/test.html',
data: data,
success: function(data){
$('.result').html(data)
},
});
is Equivalent to
$.post('ajax/test.html', function(data) {
$('.result').html(data);
});
Try their docs. They are quite good. http://api.jquery.com/jQuery.post/
$.post is a shorthand Ajax function, which is equivalent to:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
Can't say why it is slow. Have you tried inspecting your response times from your server?
$.post()
and $.get()
are both shorthand functions provided by jQuery that are really just calling $.ajax()
.
Additionally, I can assure you that AJAX calls do not take 4 to 5 seconds on the first call as a warmup penalty.