简体   繁体   English

jQuery / JavaScript 变量范围

[英]jQuery / JavaScript variable scope

 var field1;
 var field2;   
     function setUserFields() {
            $.ajax({
                type: "POST",
                url: "url",
                dataType: "xml",
                complete: parseXml
            });   
     } 
    function parseXml {
      $(xml.responseXML).find("myValue").each(function()
      {
          field1 = $(this).attr('attr1');
          field2 = $(this).attr('attr2');
          alert(field1 + ' ' field2); //shows correct values
      });
    }
 setUserFields();    

$(function() {
     alert(field1); //undefined in IE and Chrome | Gives correct value in FireFox
     alert(field2); //undefined in IE and Chrome | Gives correct value in FireFox
})

I am not posting the exact code that I am running since the code is fairly complex.我没有发布我正在运行的确切代码,因为代码相当复杂。 If there are syntax errors in the code posted please disregard them as these are not the cause of my problem.如果发布的代码中有语法错误,请忽略它们,因为这些不是我问题的原因。 The code works as expected in Firefox but not IE or Chrome.该代码在 Firefox 中按预期工作,但不适用于 IE 或 Chrome。 Also, I can verify in Firebug lite that the order the code runs shouldn't be causing a problem.此外,我可以在 Firebug lite 中验证代码运行的顺序不应该导致问题。 What I am trying to do is call a web service, parse the results and store the needed information in a global variable for use in later functions that I can only call after the DOM is finished loading.我想要做的是调用一个 web 服务,解析结果并将所需的信息存储在一个全局变量中,以便在以后的函数中使用,我只能在 DOM 完成加载后调用这些函数。 I run the setUserFields function before the document is loaded.我在加载文档之前运行setUserFields函数。 The function gets called and sets the variables but the variables are only available in the scope of parseXML() .该函数被调用并设置变量,但变量仅在parseXML()的范围内可用。 Since I have declared the variables outside the scope of all of the functions and am setting the variables inside the parseXML function I would expect that the variables would be set globally.由于我已经声明了所有函数范围之外的变量,并且正在parseXML函数内设置变量, parseXML我希望这些变量将被全局设置。 However, only in firefox can I access the variables without them being undefined.但是,只有在 Firefox 中,我才能访问变量而不定义它们。 I am pretty new to the javascript arena so I may be missing an obvious pitfall.我对 javascript 领域还很陌生,所以我可能会错过一个明显的陷阱。 I tried googling for a few hours without any luck.我尝试了几个小时的谷歌搜索,但没有任何运气。 Any help would be greatly appreciated.任何帮助将不胜感激。

This is not a scope issue.这不是范围问题。 This might be due to the asynchronous nature of AJAX calls.这可能是由于 AJAX 调用的异步特性。

The first letter in Ajax stands for "asynchronous," meaning that the operation occurs in parallel and the order of completion is not guaranteed. Ajax 中的第一个字母代表“异步”,意思是操作并行发生,完成的顺序没有保证。 The async option to $.ajax() defaults to true, indicating that code execution can continue after the request is made. $.ajax() 的 async 选项默认为 true,表示发出请求后可以继续执行代码。 Setting this option to false (and thus making the call no longer asynchronous) is strongly discouraged, as it can cause the browser to become unresponsive.强烈建议不要将此选项设置为 false(从而使调用不再是异步的),因为它会导致浏览器无响应。

jQuery.ajax()

This is mostly a timing issue.这主要是时间问题。

Your AJAX code is not getting executed by the time you call these variables当您调用这些变量时,您的 AJAX 代码尚未执行

 alert(field1); //undefined in IE and Chrome | Gives correct value in FireFox
 alert(field2); /

To fix the problem make the AJAX call in the document.要解决此问题,请在文档中调用 AJAX。 Ready and after that you can alert filed1 and field2 ,准备好之后,您可以提醒filed1field2

I am very sure this is the timing issue due to the asynchronous nature of AJAX.我很确定这是由于 AJAX 的异步性质造成的计时问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM