【问题标题】:Cannot pass data to getJSON even with closures即使使用闭包也无法将数据传递给 getJSON
【发布时间】:2016-05-15 01:19:17
【问题描述】:

不幸的是,我在 SO 上解决了这个问题的许多解决方案,但没有一个对我有用。

我想在我的页面上动态加载和放置一些元素。 HTML 由其中一个脚本生成,生成的模板(用于向其中注入数据)传递给 getJSON 方法。我想在我的回调方法中填充该模板,然后将其添加到正文中。

问题在于 getJSON 函数。我试图通过闭包和绑定传递面板值,但它们都不起作用。

init: function(panel) {


        // closure
        $.getJSON("scripts/getZones.php",
            (function () { // closure
                var p = panel;
                return function(zoneData) {
                    if (zoneData.status == true) {
                        $.each(zoneData.values, function(i, item) {
                            console.log("Module.Control_manual: Adding zone " + item.id);
// filling panel with data here - but panel is undefined
                        });
                    } else {
                        // error!
                        console.log("Module.Control_manual: Error: " + item['message']);
                    }
                }
            })
        );

    }

我的代码有什么问题?谢谢!

【问题讨论】:

  • 除此之外,您不会立即调用 (function () { // closure .... }) 函数。您的代码没有显示在您的function(zoneData) 中尝试使用panelp 的任何部分。
  • 您需要提供一个工作代码,至少是一个更完整的代码,以便人们可以重现您的问题,没有这个,您将让我们猜测,我们没有人可以给您正确的答案
  • 你为什么在 ajax 请求中使用自调用函数?
  • 除此之外zoneData 将是您当前代码中的问题:如果panel 在您的代码中的那个位置是undefined,那么它或者在您的$.getJSON("scripts/getZones.php", 之前已经是undefined 并且调用init 或在init 函数中的某处将其设置为undefined 时,您做错了。所以首先检查一下console.dir(panel);会在这里显示什么:init: function(panel) { console.dir(panel);

标签: javascript jquery json closures


【解决方案1】:

对于闭包,你必须执行你的函数,然后getJSON函数才能得到正确的回调函数

init: function(panel) {


    // closure
    $.getJSON("scripts/getZones.php", 
        // closure, you must execute it first, then the `getJSON` function could get the right callback function
        (function() { 
            var p = panel;
            return function(zoneData) {
                if (zoneData.status == true) {
                    $.each(zoneData.values, function(i, item) {
                        console.log("Module.Control_manual: Adding zone " + item.id);

                    });
                } else {
                    // error!
                    console.log("Module.Control_manual: Error: " + item['message']);
                }
            }
        }());
    );

}

此外,你应该只在你想存储panel时才使用这种关闭方式,否则你可以只使用面板而不是回调。

【讨论】:

  • 那是我的问题 - 缺少 () 用于执行关闭。谢谢!
【解决方案2】:

或者你可以使用绑定方法,像这样将参数传递给回调函数:

    function doAfterJson(panel){
                var p = panel;
                    return function(zoneData) {
                        if (zoneData.status == true) {
                            $.each(zoneData.values, function(i, item) {
                                console.log("Module.Control_manual: Adding zone " + item.id);
    // filling panel with data here - but panel is undefined
                            });
                        } else {
                            // error!
                            console.log("Module.Control_manual: Error: " + item['message']);
                        }
                    }
                }

    }

    //use 'bind(scope,parameter)' to pass scope and parameter to callback function

init: function(panel) {


        // closure
        $.getJSON("scripts/getZones.php",doAfterJson.bind(this,panel));
    }

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多