I wanna intercept a submit via jQuery and first check if a file is present on the server. If it's present continue with the request, if not display a message and dont send the request. This is what I have:
$("#methodForm").submit(function(e){
checkIndex('upload/segments.gen').done(function() {
return true;
}).fail(function () {
e.preventDefault();
alert("No index present!");
return false;
});
});
function checkIndex(file){
return $.ajax({
url : file,
type:'HEAD'
});
}
You can't return out of a callback to an asynchronous method(such as ajax). Instead, prevent the submit all together, then submit it when you are ready.
$("#methodForm").submit(function(e){
e.preventDefault();
var form = this;
checkIndex('upload/segments.gen').done(function() {
form.submit(); // submit bypassing the jQuery bound event
}).fail(function () {
alert("No index present!");
});
});