在$ .getJSON函数中设置的变量只能在函数内访问 [英] Variables set during $.getJSON function only accessible within function

查看:176
本文介绍了在$ .getJSON函数中设置的变量只能在函数内访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能更多是一个范围问题。我试图在$ .getJSON函数中设置一个JSON对象,但我需要能够在回调之外使用该对象。

This may be more of a scoping question. I'm trying to set a JSON object within a $.getJSON function, but I need to be able to use that object outside of the callback.

var jsonIssues = {};  // declare json variable

$.getJSON("url", function(data) {
    jsonIssues = data.Issues;
});

// jsonIssues not accessible here

类似这样的问题在另一篇文章中提到,我的共识是,我需要做的任何JSON对象需要在回调函数内完成,并且不能在任何其他地方访问。真的没有办法,我可以继续访问/操纵JSON对象的$ .getJSON回调之外?如果返回变量或设置全局变量呢?

A similar question like this one was asked in another post, and the consensus was that anything I need to do with the JSON objects needs to be done within the callback function, and cannot be accessed anywhere else. Is there really no way that I can continue to access/manipulate that JSON object outside of the $.getJSON callback? What about returning the variable, or setting a global?

我会感谢任何帮助。这似乎不对。

I'd appreciate any help. This just doesn't seem right...

更新:

将$ .ajax()async设置为false,并运行相同的代码,没有运气。我尝试的代码如下:

Tried setting the $.ajax() async setting to false, and running through the same code, with no luck. Code I tried is below:

var jsonIssues = {};  // declare json variable

$.ajax({ async: false });

$.getJSON("url", function(data) {
    jsonIssues = data.Issues;
});

// jsonIssues still not accessible here

一对夫妇回应一个全局变量应该工作正常。我应该澄清,所有这些代码在 $(document).ready(function(){。)设置全局变量,我应该在文档之前声明它。就像这样:

Also, I've had a couple responses that a global variable should work fine. I should clarify that all of this code is within $(document).ready(function() {. To set a global variable, should I just declare it before the document.ready? As such:

var jsonIssues = {};

$(document).ready(function() {

  var jsonIssues = {};  // declare json variable

  $.getJSON("url", function(data) {
      jsonIssues = data.Issues;
  });

  // now accessible?
}

我的印象是,document.ready中声明的变量应该在document.ready的任何部分全局可访问和修改,包括像$ .getJSON回调函数这样的子函数我可以需要阅读javascript变量范围,但似乎没有一个容易实现我想要的。感谢所有的回应。

I was under the impression that that a variable declared within document.ready should be "globally" accessible and modifiable within any part of document.ready, including subfunctions like the $.getJSON callback function. I may need to read up on javascript variable scoping, but there doesn't seem to be an easy to achieve what I'm going for. Thanks for all the responses.

UPDATE#2:
对于下面的答案,我使用$ .ajax 而不是 .getJSON,并且实现了我想要的结果。代码如下:

UPDATE #2: Per comments given to answers below, I did use $.ajax instead of .getJSON, and achieved the results I wanted. Code is below:

var jsonIssues = {};
    $.ajax({
        url: "url",
        async: false,
        dataType: 'json',
        success: function(data) {
            jsonIssues = data.Issues;
        }
    });

    // jsonIssues accessible here -- good!!

对我的答案几个后续评论(我感谢他们)。我这样做的目的是最初加载一个JSON对象,然后用户可以从中移除并保存的问题列表。但这是通过网页上的后续互动完成的,我无法预见用户将如何处理回调中的JSON对象。因此,一旦回调完成,需要使其可访问。有没有人看到我的逻辑在这里的缺陷?认真地,因为可能有一些我没有看到...

Couple follow-up comments to my answers (and I appreciate them all). My purpose in doing this is to load a JSON object initially with a list of Issues that the user can then remove from, and save off. But this is done via subsequent interactions on the page, and I cannot foresee what the user will want to do with the JSON object within the callback. Hence the need to make it accessible once the callback complete. Does anyone see a flaw in my logic here? Seriously, because there may be something I'm not seeing...

此外,我正在阅读.ajax()jQuery文档,它说,设置异步to false同步加载数据在请求处于活动状态时阻止浏览器。最好在需要同步时阻止用户通过其他方式进行交互。

Also, I was reading through the .ajax() jQuery documentation, and it says that setting async to false "Loads data synchronously. Blocks the browser while the requests is active. It is better to block user interaction by other means when synchronization is necessary."

一个想法我应该如何阻止用户交互,而这是怎么回事?为什么会这样的关注?再次感谢所有回应。

Does anyone have an idea how I should be blocking user interaction while this is going on? Why is it such a concern? Thanks again for all the responses.

推荐答案

$。getJSON 。也就是说,在 $。getJSON 提取和解析数据并调用您的回调后,调用后的代码被执行。

$.getJSON is asynchronous. That is, the code after the call is executed while $.getJSON fetches and parses the data and calls your callback.

因此,给定:

a();

$.getJSON("url", function() {
    b();
});

c();

a b c 可以是 abc

>解决方案?

The solution?

使请求同步而不是异步:

Make the request synchronous instead of asynchronous:

a();

$.ajax({
    async: false,
    url: "url",
    success: function() {
        b();
    }
});

c();



重组代码



b 后调用 c

a();

$.getJSON("url", function() {
    b();

    c();
});

这篇关于在$ .getJSON函数中设置的变量只能在函数内访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆