1
Within a function callback()
one click required to make at least 3 requests.
I’ve separated that into three functions: (func1(), func2() e func3())
and each of them makes a request ajax ($.get)
returning to me true
or false
. If the func1()
return true to func2()
can be called and if the func2()
return true to func3()
can be called. If any of the func()
return me false I cannot proceed with the func
next.
How can I do that, why this way it is not returning true or false, it is returning undefined
.
Link in jsfiddle: Code
$('#chamar').bind('click',
function () {
$('#form').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
} else {
if(func1()==false){
//aqui ele ja deveria finalizar e não fazer a proxima requisição
alert("Função 1 falhou");
}
if(func2()==false){
//aqui ele ja deveria finalizar e não fazer a proxima requisição
alert("Função 2 falhou");
}
if(func3()==false){
//aqui ele ja deveria finalizar e não fazer a proxima requisição
alert("Função 3 falhou");
}
alert("Todas as requisições foram feitas");
}
})
});
function func1() {
$.get('consulta1.php?type=dado&action=get', {
id: $('#id').val(),
},function (e) {
e = $.parseJSON(e);
if (e[0].status) {
alert("DEU VERDADEIRO");
return true;
} else {
alert("DEU FALSO");
return false;
}
}).fail(function () {
alert("DEU FALSO");
return false;
});
}
function func2() {
$.get('consulta2.php?type=dado&action=get', {
id: $('#id').val(),
},function (e) {
e = $.parseJSON(e);
if (e[0].status) {
alert("DEU VERDADEIRO");
return true;
} else {
alert("DEU FALSO");
return false;
}
}).fail(function () {
alert("DEU FALSO");
return false;
});
}
function func3() {
$.get('consulta3.php?type=dado&action=get', {
id: $('#id').val(),
},function (e) {
e = $.parseJSON(e);
if (e[0].status) {
alert("DEU VERDADEIRO");
return true;
} else {
alert("DEU FALSO");
return false;
}
}).fail(function () {
alert("DEU FALSO");
return false;
});
}
Hello Isac thank you for answering. I did using this approach but it turns out that it is going into some kind of loop. When I click the button for the first time it gives the error "Function 1 failed". When I click the button a second time it appears 2 times the error "Function 1 failed", when I click the button for 3 time it gives the error 3 times "Function 1 failed" and so on. What can it be?
– L.J
I looked at Chrome’s Developer Tools and it’s like it’s repeating the requests because it has several. When I click the second time, it makes two requests. When I click the third time it makes 3 requests and so on.
– L.J
@L.J That’s why every time the
#chamar
is clicked sets a new click Handler, something that has passed me unnoticed in your code. What you want to do is set the$('#form').validator().on('submit'
out of the click of#chamar
and at the click of#chamar
just send the form by doing$('#form').submit()
. The goal of the button#chamar
is validate and submit the form if it is valid right?– Isac
That.. before sending I need to check if the form is valid. I just use the 'Submit' pq I need to validate the fields and check if everything is ok. So much so that my form action is empty. The form is submitted by func1, func2 and func3. Then to use Validator I have to pass the ide of the form. Validator()...I don’t know if I could do with $("#call"). Validator().
– L.J
So I don’t need to use $('#form'). Submit(). I submit through the functions' ajaxes 1, func2 and func3.
– L.J