开发者

Problem with passing data from anonymous function in Ajax and jQuery

I need to validate a user and password before a form is sent. For that I use jQuery and Ajax. But I have problems passing the 'estado'value outside the anonymous function.

This is the code:

function tx_oriconvocatorias_pi1_validarFormPost(){
    var x=document.forms["form_postulante"]["pass_un"];
    if (x.value==null || x.value==""){
        alert("Por favor ingrese su contraseña.");
        return false;
    }
    var $j = jQuery.noConflict();
    var cun = $j('#correo_un').val();
    var pun = $j('#pass_un').val();
    var estado = null;
    $j.ajax({
        type: 'POST',开发者_如何学Go
        data: 'eID=ori_convocatorias_formPost&correo_un='+cun+'&pass_un='+pun,
        success: function(datos){
            if(datos!=1){
                alert ("La contraseña ingresada no es válida"); 
            }
            estado = datos;
            alert(estado);
        },
    });
    alert(estado);
    if (estado!=1){
        return false;
    }
}

I notice that the first alert(estado)(outside) returns null and then the second alert(estado) (inside) returns 'datos', so the last alert(estado) in the code is executed first and always my function returns false.

I don't know how to evaluate 'estado' after execute the Ajax code and not before.

Thanks in advance for the help.


You can't. You are doing an asynchronus request so your function ends before the result of the ajax call has returned. You can force the call to be synchronus :

$j.ajax({
        type: 'POST',
        data: 'eID=ori_convocatorias_formPost&correo_un='+cun+'&pass_un='+pun,
        async: false,//here you are synchrone
        success: function(datos){

or you can return false, and submit your form manually.


The reason the first alert(estado) is null and the second alert(estado) is 'datos', is because ajax is asynchronous. This means that even though you are calling $j.ajax, the code is still going through and evaluating the rest of the function.

I think this might be something more of what you're looking for:

function tx_oriconvocatorias_pi1_validarFormPost(){
    var x=document.forms["form_postulante"]["pass_un"];
    if (x.value==null || x.value==""){
        alert("Por favor ingrese su contraseña.");
        return false;
    }
    var $j = jQuery.noConflict();
    var cun = $j('#correo_un').val();
    var pun = $j('#pass_un').val();
    var estado = null;
    $j.ajax({
        type: 'POST',
        data: 'eID=ori_convocatorias_formPost&correo_un='+cun+'&pass_un='+pun,
        success: function(datos){
            if(datos!=1){
                alert ("La contraseña ingresada no es válida"); 
            }
            estado = datos;
            alert(estado);
        },
        error: function(datos) {
            return false;
       }
    });
}


You have to use synchronous call in order to achieve this. But with synchronous ajax calls the browser will not respond untill the response comes keep this in mind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜