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

查看:21
本文介绍了在 $.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 对象做的任何事情都需要在回调函数中完成,并且不能在其他任何地方访问.我真的没有办法在 $.getJSON 回调之外继续访问/操作那个 JSON 对象吗?返回变量或设置全局变量怎么样?

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() 异步设置设置为 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() { 中.要设置全局变量,我应该在 document.ready 之前声明它吗?因此:

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.

更新 #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 文档,它说将 async 设置为 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();

abc的调用顺序可以是abc(什么想要,在这种情况下)或acb(更有可能实际发生).

The order of the calls of a, b, and c may be either a b c (what you want, in this case) or a c b (more likely to actually happen).

解决方案?

使请求同步而不是异步:

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天全站免登陆