试图保存AJAX()调用对变量的响应,但该变量返回空字符串

时间:2014-05-28 07:29:34

标签: javascript jquery ajax jquery-ajaxq

我试图在javascript变量中保存AJax()调用的响应,但是当我将值附加到div时,此变量返回空。

这是我的脚本代码

<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

正如您在上图中看到的,我在控制台中收到了响应。但是,当我尝试将响应保存在上面脚本中提到的var vajx时,它的空白可能我知道原因。

我是Ajax()的新手,所以需要帮助

更新

在查看下面给出的一些示例并尝试我自己之后,我将如何解决它。

答案

<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>

请注意,现在我在成功中启动弹出窗口:function

提前谢谢

3 个答案:

答案 0 :(得分:1)

试试这个:

//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");
}

然后你可以像这样改变你的if检查:

答案 1 :(得分: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;
}

});

答案 2 :(得分:0)

$.ajax有一个success处理程序,用于处理从服务器收到的响应。所以你可以这样做:

$.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
      }
});