I have created a function called save , when i call save function, I am getting success undefined or object Object
UPDATES:
updated to get he values jqxhr object that ajax returns
function save() {
return $.ajax({
type: "POST",
url: "foo.json",
data: json_data,
contentType: 'application/json',
success: function (data, textStatus, xhr) {
$('<div id="loading">Loading...</div>').insertBefore('#form');
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}
$(document).ready(function () {
$(function () {
$("#save").click(function () {
var jqxhr = save();
alert("success " + jqxhr.success);
alert("status " + jqxhr.status);
alert("status " + jqxhr.readyState);
});
});
});
Ninja edit by OP
First of all, there is noreturnstatement within yoursavefunction, so it works as expected by returningundefinedvalue.Secondly, it won’t work that way. You need to return the
$.ajaxcall (which itself, returns ajqXHR object, where you can hook in and setup code for different events. Afterall, by default an Ajax request runs asyncronously.in save()
and later…
Learn more about jQuerys Ajax and Deferred objects here and here.