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');
        }
    });
    
    评论

报告相同问题?