1

我正在尝试将 AJax() 调用的响应保存在 javascript 变量中,但是当我将值附加到 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> 

在此处输入图像描述

正如您在上图中看到的,我在控制台中得到了响应。但是当我尝试将响应保存在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>

请注意,现在我正在成功内启动弹出窗口:函数

先感谢您

4

3 回答 3

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

});

于 2014-05-28T07:40:11.083 回答
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 检查:

于 2014-05-28T07:38:32.537 回答
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
      }
});
于 2014-05-28T07:35:45.383 回答