Pretty straightforward, but it doesn't work the way it should.
$(function(){
(function(){
/**
* @return boolean Depending on success
*/
function saveOnServer(data){
var result = false;
$.ajax({
url : "/post/write.ajax",
data : data,
success : function(response){
if (response == "1"){
alert('Response is 1. OK');
result = true;
}
}
});
return result;
}
$("#save").click(function(e){
e.preventDefault();
// This inserts some stuff (on the server), but always returns FALSE
if ( saveOnServer($("form").serialize())) ){
alert('1'); // This expected to be alerted
} else {
alert('0'); // <- But... This will be alerted
}
});
})($);
});
The problem is that response
var always returns 1
, and it alerts Response is 1. OK
as well. But...
result
is always FALSE
. Why?