I have the following code:
var bduplicate;
bduplicate = false;
bduplicate = check_for_duplicate_rule().done(function (result) {
console.log("result from check for duplicate" + result);
if(result == 'true') {
$('#validation_error').html("A similar rule already exists");
return result;
}
});
console.log(bduplicate);
if(bduplicate.success == 'true') {
console.log('im exiting');
return false; //exit this function
}
And here's the definition of the async method itself:
function check_for_duplicate_rule() {
var parameters = {
num: $('#num').val(),
condition: $('#condition').val(),
cdidnumber: $('#cdidnumber').val()
}
return $.getJSON(
url = aURLdefinedsomewhere,
parameters,
function (data) {
//if (data=='true') {
//$('#validation_error').html("A similar rule already exists");
//}
} //end data
); //end getJSON
}
Problem:
The system never enters into the IF statement that prints to the console "im entering". But it does correctly change the text of the validation_error element. And it prints the following in the console: result from check for duplicate:true ". This leads me to believe that the way I'm testing for the results stored in bduplicate is incorrect.
What I've tried so far:
I included the following line in the code:
console.log(bduplicate);
to see what I'm getting back. The console prints the contents of the object and includes a bunch of different properties. There's one called "responseText" property, it says responseText: ""true"↵" Could this be the problem? Although, if it is, it doesn't explain why the test to set the validation error element passes.
In case it helps, the server is the data for the ajax call like so:
return json.encode(true)
Questions:
In addition to trying to figure out why I can't get the code to display the results from the "console.log('im exiting')" command, I'm also wondering if the way I'm returning the json data is correct?
I read somewhere that technically, it should look like this:return json.encode('success: true') If this is correct, how would I test for true in the front end?
Can anyone point me to some documentation on the object that's returned by the .done() method? I've been searching the jquery API but haven't found anything yet. I'm probably not searching with the right key words.
Thanks.