[ACCEPTED]-jquery - creating a generic ajax function-jquery
This is what's comes to my mind with "generic" - but 1 it's still asynchronous:
function CallMethod(url, parameters, successCallback) {
//show loading... image
$.ajax({
type: 'POST',
url: url,
data: JSON.stringify(parameters),
contentType: 'application/json;',
dataType: 'json',
success: successCallback,
error: function(xhr, textStatus, errorThrown) {
console.log('error');
}
});
}
CallMethod(url, pars, onSuccess);
function onSuccess(param) {
//remove loading... image
//do something with the response
}
I know I've gotten [object][Object] back 4 before....are you sure your application 3 is returning json? If not, Json.NET makes 2 it pretty easy. You might need to call $.parseJSON 1 too.
$.ajax is a generic jQuery function.
You 11 want to do something like this:
$(function() {
function LoadPersons(data) {
// do something with data
}
$.ajax({
type: 'POST',
url: url,
data: JSON.stringify(parameters),
contentType: 'application/json;',
dataType: 'json',
success: function(result) {
// do something with persons (data)
// e.g. LoadPersons(data);
}
});
});
In text form:
Your 10 ajax success should be the bit that's responsible 9 for doing something with the data after 8 it's successfully been returned. Your previous 7 method tries to return data before the AJAX 6 method has necessarily been completed. You 5 can just add the $.ajax method directly 4 inside $(function() { .. }) so it's called 3 upon page load.
Also, I recommend using a 2 tool such as Firebug to examine the output 1 / response of AJAX calls.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.