1

I am trying to save the reponse of an AJax() call in a javascript variable but this variable returns empty when I append the value to a div .

here is my script code

<script>
/*<![CDATA[*/
$(document).ready(function(){       
$("#abusoForm #enviar").livequery("click",function(e){e.preventDefault();
 console.log("Click is working");
    var hidden = $('#mensajeAbuso').val();
    var category = $('#opcmarcar').val();
    var name=$('#nombre').val();
    var phone=$('#telefono').val();
    var mail=$('#email').val();
    var cf_mail=$('#confirma_email').val();
    var k="<?php echo $this->config->defaultLanguage?>";

    var url="somedomain.com/index.php?param=value";

    //url = 'proxy.php?url='+url;
    var otro = $('#otro_email').val();
    var E=$("#abusoForm #enviar").val();

var alto_height = $(window).height();
    alto_height = alto_height/4;

//Ajax call happening here 
var vajx =$.ajax({url:url,type:"POST",data:{ 'h':hidden,'c': category,'n':name,'p':phone ,'m':mail,'cm':cf_mail,'otro1':otro,"enviar":E,async:false}}).responseText;


//Now I have to use the variable vajx to post a message about the submition of the form ;
if(vajx!=""){
    $("div.error_mensajeria").css("display","none");
    $(".appendcontentAbuso").html(vajx);
    $('#mDialogAbuso').css("height",alto_height);
    $("#mDialogAbuso").popup();
    $("#mDialogAbuso").popup("open");

} 

})
});
/*]]>*/</script> 

enter image description here

As you can see in the above image I am getting the response in the console . But when i try to save the response in the var vajx like mentioned in the script above its empty may I know why .

I am very new to Ajax() so need help

UPDATE

After looking into some examples given below and trying my own here is how I could fix it .

Answer

<script>
/*<![CDATA[*/
$(document).ready(function(){       
$("#abusoForm #enviar").livequery("click",function(e){e.preventDefault();
 console.log("Click is working");
    var hidden = $('#mensajeAbuso').val();
    var category = $('#opcmarcar').val();
    var name=$('#nombre').val();
    var phone=$('#telefono').val();
    var mail=$('#email').val();
    var cf_mail=$('#confirma_email').val();
    var k="<?php echo $this->config->defaultLanguage?>";

    var url="http://wstation.inmotico.com/index.php?page=avisoajax&type=spam&im_action=reportAbuse&im_core=showAds";

    //url = 'proxy.php?url='+url;
    var otro = $('#otro_email').val();
    var E=$("#abusoForm #enviar").val();

var alto_height = $(window).height();
    alto_height = alto_height/4;

//Ajax call happening here 
//var vajx =$.ajax({url:url,type:"POST",data:{ 'h':hidden,'c': category,'n':name,'p':phone ,'m':mail,'cm':cf_mail,'otro1':otro,"enviar":E,async:false}}).responseText;
var result = ''; // declare a var here
var vajx = $.ajax({
    url: url,
    type: "POST",
    data:{ 'h':hidden,'c': category,'n':name,'p':phone    ,'m':mail,'cm':cf_mail,'otro1':otro,"enviar":E,async:false},
    success: function(data){
        $(".appendcontentAbuso").html(data); // <-----------change here
   $('#mDialogAbuso').css("height",alto_height);
   $("#mDialogAbuso").popup();
   $("#mDialogAbuso").popup("open");

    }
});
/*vajx.done(function (data) {
    result = data; // <-----------change here
});

if(result != ""){ // <---------------change here
  // $("div.error_mensajeria").css("display","none");
   $(".appendcontentAbuso").html(result); // <-----------change here
   $('#mDialogAbuso').css("height",alto_height);
   $("#mDialogAbuso").popup();
   $("#mDialogAbuso").popup("open");
}*/
console.log(data);
 //$('#ajxResponse').html(vajx);
})
});
/*]]>*/</script>

Please notice that now I am initiating the popup inside the success: function

Thank you in advance

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

3 Answers3

1

Try this:

//Ajax call happening here 
var result = ''; // declare a var here
var vajx = $.ajax({
    url: url,
    type: "POST",
    data: {
        'h': hidden,
          .....
        async: false
    }
});
vajx.done(function (data) {
    result = data; // <-----------change here
});

if(result != ""){ // <---------------change here
   $("div.error_mensajeria").css("display","none");
   $(".appendcontentAbuso").html(result); // <-----------change here
   $('#mDialogAbuso').css("height",alto_height);
   $("#mDialogAbuso").popup();
   $("#mDialogAbuso").popup("open");
}

and then you can change your if check little bit like this:

Jai
  • 74,255
  • 12
  • 74
  • 103
  • no bro still its empty When I tried doing console.log(vajx); in the console I see its an object not a normal variable . **Object { readyState=4, responseText="E-mail incorrecto", status=200, more...}** – Vikram Anand Bhushan May 28 '14 at 07:56
  • @VikramAnandBhushan checkout the updated one. – Jai May 28 '14 at 08:04
  • when I tried this one still it give empty string . I tried console.log(result) which shows empty string in the console log – Vikram Anand Bhushan May 28 '14 at 08:12
  • Atlast made it work like this .success: function(data){ $(".appendcontentAbuso").html(data); // <-----------change here $('#mDialogAbuso').css("height",alto_height); $("#mDialogAbuso").popup(); $("#mDialogAbuso").popup("open"); } – Vikram Anand Bhushan May 28 '14 at 10:35
1
var vajx;

$.ajax({
url: url,
type:"POST",
data:{ 'h':hidden,'c': category,'n':name,'p':phone    ,'m':mail,'cm':cf_mail,'otro1':otro,"enviar":E,async:false}
)
.done(function( data ) {
 vajx = data;
}

});

Innovation
  • 1,514
  • 17
  • 32
0

$.ajax has a success handler which handles the response received from the server. So you could do something like this:

$.ajax({
      url:url,
      type:"POST",
      data:{ 'h':hidden,'c': category,'n':name,'p':phone ,'m':mail,'cm':cf_mail,'otro1':otro,"enviar":E},
      async:false,
      success:function(ret)
      {
          //the response received from url will be stored in "ret"
          var vajx = ret;
          // use your conditions here now
      }
});
asprin
  • 9,579
  • 12
  • 66
  • 119