从 $.getJSON() 方法获取数据

发布于 2024-07-26 22:48:49 字数 237 浏览 6 评论 0原文

我正在编写一个 Web 应用程序,需要初始化一些通过 $.getJSON() 方法拉取的参数。

$.getJSON("../config/", function(data)
{
     console.debug(data);
}

现在,由于这些值将在整个脚本中全局使用,并且不会直接触发事件(这是我在文档中可以找到的 $.getJSON() 的唯一实现),我如何返回或检索此回调数据?

I'm writing a web application and need to initialize some parameters that I'm pulling via the $.getJSON() method.

$.getJSON("../config/", function(data)
{
     console.debug(data);
}

Now since these values will be used globally throughout the script and will not be triggering an event directly (which is the only implementation of $.getJSON() I could find in the documentation), how can I returning or retrieve this callback data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2024-08-02 22:48:49

最好的选择是坚持使用回调技术。

有两种真正的方法可以使其发挥作用,两者本质上是相同的。

$.getJSON("../config/", function(data) {
        SomeObject.config = data; 
        SomeObject.load();   # assuming load tells some-object that it now has data and to get cracking
});

或者

$.getJSON("../config/", function(data) {
        SomeObject.load( data );   # SomeObject sets itself up and starts doing its thing
});

尝试以同步方式使用 $.getJSON(即:让它返回一个值)只会让您和使用您网站的人都以泪水和痛苦告终,因为同步连接有一种趋势阻止整个 UI。 :)

就目前情况而言,异步执行此类操作

var i = null;                              #1
$.getJSON("../config/", function(data) {   #2
        i = data;                          #3
});                                        #4
some_function_with(i);                     #5

是行不通的,因为几乎可以保证第 5 行在第 3 行之前执行。

Your best bet is to stick with the callback technique.

There are 2 real ways to make it work, both are essentially the same.

$.getJSON("../config/", function(data) {
        SomeObject.config = data; 
        SomeObject.load();   # assuming load tells some-object that it now has data and to get cracking
});

or

$.getJSON("../config/", function(data) {
        SomeObject.load( data );   # SomeObject sets itself up and starts doing its thing
});

Trying to use $.getJSON in a synchronous way ( ie: having it return a value ) will only end in tears and misery for both you and the people using your site, because Synchronous connections have a tendency to block the entire UI. :)

As it stands, doing anything like this asynchronously

var i = null;                              #1
$.getJSON("../config/", function(data) {   #2
        i = data;                          #3
});                                        #4
some_function_with(i);                     #5

Will not work, because line 5 is almost guaranteed to execute before line 3.

最丧也最甜 2024-08-02 22:48:49

Kent Fredric:我不确定你的方法是否更好,所以如果你的方法更好,请告诉我如何,我会接受你的解决方案而不是我自己的解决方案,但这就是我的做法:

 var my_data = null;

$.ajax(
{
    url: "../config/",
    dataType: "json",
    async: false,
    success: function(data)
    {
        my_data = data;
    }
});

也谢谢你 RichieHindle,我不知道可以在不返回的情况下替换函数外部的变量。

Kent Fredric: I'm not sure if your approach might be better, so if your method is better let me know how and I'll accept your solution over my own, but this is how I did it:

 var my_data = null;

$.ajax(
{
    url: "../config/",
    dataType: "json",
    async: false,
    success: function(data)
    {
        my_data = data;
    }
});

Also thank you RichieHindle, I didn't know it was possible to replace variables outside of functions without return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文