(function($)
{
$.fn.makeAjaxPostCall = function(options)
{
var settings = $.extend( true,{}, $.fn.makeAjaxPostCall.defaults, options );
$.ajax({
type: 'POST',
url: settings.url,
data: settings.data,
success: function(data, textStatus, xhr){
if (xhr.readyState === 4 && xhr.status === 0) {
alert('Network Error');
}
else {alert(data);
settings.callback.call(data);
}
},
error: function(xhr, textStatus, errorThrown) {
if (textStatus !== null) {
alert("error: " + textStatus);
} else if (errorThrown !== null) {
alert("exception: " + errorThrown.message);
}
else {
alert ("error");
}
}
});
};
$.fn.makeAjaxPostCall.defaults = {
type : 'POST',
url : '',
data : {},
obj : $(document),
callback : function(data){}
};
})(jQuery);
This is my ajax function. And I am calling this function as
$(document).makeAjaxPostCall({
type : 'POST',
url : 'UpdateNodeStatus',
data : {
workshopid : 1,
userid : 1001,
level : 5,
nodeid : 10,
resolutionid : 1
},
callback : function(data){
alert(data);
}
});
after post happened I am returning 'true' from servlet.
response.setContentType("text");
PrintWriter pw = response.getWriter();
pw.print(true);
I am getting data as 'true' in 'success', but as 'undefined' in callback.
How do I pass 'data' from 'success' to 'callback' function to use it in there?
settings.callback(data);?call()should work as well, but all of this looks like just jumping through a lot of hoops to create a new ajax function that does just about the same as the one already available in jQuery ?