braz braz - 2 months ago 6
Javascript Question

Ajax inside Ajax somehow strange behaviour

I have a Ajax-Call inside a Ajax-Call, everything "seems" to work fine. In console I can see, both calls are executed and get a return.

But somehow, i can't use the returned result from the second call(?)

$.ajax({
type: "POST",
url: "register/checkEmail/"+email,
success: function(result){

if(result == "TRUE") {
$('#regMsg').html('Ein User mit dieser Email ist bereits registriert!');
$('#regMsg').slideDown();

// NO ERROR - REGISTER USER
} else {
$('#regMsg').slideUp();
var inputs = $('#regForm :input').serializeArray();

alert('ok');
$.ajax({
method: "POST",
url: "register/save",
data: inputs,
dataType: 'json',
success: function(result){
alert('ddok');
}
});

}

}
});


the first
alert()
is beeing displayed, the secont is not, although the second call is executed correctly(?) why is that?

Pat Pat
Answer

Simple - the second call's response did not return back to the ajax i.e error/fail.

Add the error handling part after success to find the response. After success add

,error: function(result){
    alert('error'); 
    console.log(result);
}

If this is not the reason, then dataType: 'json', should be the culprit as your response wouldn't be in json format !!