weixin_33705053 2015-10-12 05:21 采纳率: 0%
浏览 65

jQuery中的未定义变量

Through ajax call i am updating something and returning false and true. but the problem is that before i get result value from update function the if condition that is present after update function is executed and thus it give an error undefined result and errMsg variable. If I write this code again then it works. how i can stop running if condition until my update function returns some value?

var result = update(field_name, field_value);

if (result == false) {
  alert(errMsg);
} else {
  alert(errMsg);
}

update function

function update(field_name, field_value) {
  if (field_value) {
    $.post("/restapi/vc/users/id/${user.id}/profiles/name/" + field_name + "/set?value=" + field_value, function(data, status) {
      var xmlDoc = data;
      var x = xmlDoc.getElementsByTagName("response")[0];
      var txt = x.getAttribute("status");
      if (txt == 'error') {
        var msg = xmlDoc.getElementsByTagName("message")[0];
        var errMsg = msg.childNodes[0];
        window.errMsg = errMsg.nodeValue;
        return false;
      } else {
        return true;
      }
    });
  }
}
  • 写回答

4条回答 默认 最新

  • python小菜 2015-10-12 05:24
    关注

    because update function returns a void/null. and null is == to false.

    var result = update(field_name,field_value);
    //update method is called and returns null 
    if(result == false){  //null == false is true
        alert(errMsg);  //this line will execute
    }else{
        alert(errMsg);
    }
    

    this is happening because of async nature of JavaScript. window.errMsg is being set after it is alerted. ajasx post in update method is a ajax request and its sucess method is called async.

    better way withtout using Deffered would be to use callback.

    var update = function update(field_name, field_value, callback) {
        if (field_value) {
            $.post(
                "/restapi/vc/users/id/${user.id}/profiles/name/" + field_name + "/set?value=" + field_value,
                function(data, status) {
                    var xmlDoc = data;
                    var x = xmlDoc.getElementsByTagName("response")[0];
                    var txt = x.getAttribute("status");
                    if (txt == 'error') {
                        var msg = xmlDoc.getElementsByTagName("message")[0];
                        var errMsg = msg.childNodes[0];
    
                        //its not good practice to add 
                        //transactional messages to window/global scope
                        //window.errMsg = errMsg.nodeValue;  
    
                        //return false;   
                        //instead of return call the callback
                        callback(errMsg.nodeValue);
                    } else {
                        //return true;
                        //likewise in case of success
                        callback();
                    }
                }
            );
        }
    };
    
    //and call update method like this -
    update(field_name, field_value, function(errMsg){
        if (errMsg) {
            alert(errMsg);  //alert the error message
        } else {
            alert('success');
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程