I'm currently working on implementing validation in a form, and one of the needed validations involves retrieving a value from an input field and checking its existence on the server or in a JSON file.
However, there seems to be a problem where even if the value exists and the proper return state is triggered, it still gets redirected. I suspect that the promise
might have something to do with this unexpected behavior.
This is a snippet of my code:
$("#btnclick").click(function(){
$.getJSON("locations.json", function(json) {
var locations = _.filter(json, function(o) {
return o.value == $originLocation;
});
if(locations.length <=0){
console.log("The original location doesn't exist");
//stop here
return;
}else{
console.log("Yes, it does exist");
}
});
window.location = "some link.php";
// move to another link--> redirect
});