I have a jquery $.post function lik so:
$.post(\'/test\',json_data, function(data) {
result=data
});
this fun
Check for validity inside the callback
$.post('/test',json_data, function(data) {
var result=data;
if(result === true) {
// do some ok processing...
} else {
// do not ok processing...
}
});
This is considered a bad practice but there is an async
parameter in $.ajax() that you can set to false
and wait synchronously for a result:
var result;
$.ajax({
type: 'POST',
url: '/test',
data: json_data,
async: false,
success: function(data) {
result=data;
},
dataType: 'application/json'
});
//result available here
A more idiomatic, although slightly less convenient in this case, would be to take advantage of callbacks:
function validate(okCallback, errorCallback) {
$.post('/test',json_data, function(data) {
if(data) {
okCallback();
} else {
errorCallback();
}
});
}
validate(
function() {
//proceed
},
function() {
//display validation error
}
}