【问题标题】:jquery $ajax closurejquery $ajax 关闭
【发布时间】:2012-11-06 11:01:01
【问题描述】:

我有以下函数,它接受这个频率参数并调用 api,但参数频率没有在 SUCCESS 回调中定义,我需要它。如何将该参数传递给我的回调?

 init : function(frequency) {
    $.ajax({
        url: 'api/v1/dashboard',
        type: "GET",
        dataType: "json",
        data : {username : "demo",frequency : frequency},
        success: function(data) {
            dashboard_hander.set_data(data.dashboard);
                            //here frequency is undefined

        }
    });
},

编辑 2 =====

它通过定义参数来工作 = {username : "demo",frequency : frequency}; 在 $.ajax 之外并作为数据对象传递,或在更高范围内定义频率,例如:

 var dashboard_hander = {
frequency : "",
init : function(frequency) {
    this.frequency = frequency;

       ...
    }
 } 

【问题讨论】:

    标签: jquery ajax closures


    【解决方案1】:

    只需在更高的范围内定义它。

       init : function(frequency) {
            var myData = {username : "demo",frequency : frequency};
            $.ajax({
                url: 'api/v1/dashboard',
                type: "GET",
                dataType: "json",
                data : myData,
                success: function(data) {
                    alert(myData.frequency);
                    dashboard_hander.set_data(data.dashboard);
                                    //here frequency is undefined
    
                }
            });
        },
    

    【讨论】:

    • 成功回调是在init函数执行后触发的,但是因为myData是一个对象所以有效。谢谢!!
    【解决方案2】:

    演示http://jsfiddle.net/B2h8m/2/ 这是它的代码:

    var test = {init : function(frequency) {
        (function() {
            $.ajax({
                url: 'api/v1/dashboard',
                type: "GET",
                dataType: "json",
                data : {username : "demo",frequency : frequency},
                success: function(data) {
                    alert(frequency);
    
                },
                error: function(data) {
                    alert(frequency);
    
                }
            });
        })();
    }}
    test.init(10);
    

    包装 $.ajax 的附加函数将创建一个闭包,这样您就可以访问频率。

    编辑: 此外,奇怪的是你在那个地方没有定义它。请参阅this 演示。它使用与您相同的代码。并且工作正常。不需要我添加的其他函数,因为成功回调函数也会创建一个闭包。除了您显示的代码与您有问题的代码不同之外,您的代码成功定义如下:

    success: successHandlerFunctionName
    

    在这种情况下,您可以将其更改为:

    success:function(data) {successHandlerFunctionName(data, frequency);}
    

    频率将从关闭中获取

    【讨论】:

      【解决方案3】:

      参数frequency 定义,因为它由闭包保存。只有在 ajax 调用后更改它才会消失。

      Demonstration with this code :

      <script>
      var obj = {
          init : function(frequency) {
          $.ajax({
              url: 'index2.html',
              type: "GET",
              dataType: "html",
              data : {username : "demo",frequency : frequency},
              success: function(data) {
                  alert('frequency:' + frequency);
              }
          });
      }};
      obj.init(33);
      </script>
      

      【讨论】:

        猜你喜欢
        • 2011-12-31
        • 1970-01-01
        • 2013-09-22
        • 2017-05-03
        • 2013-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-20
        相关资源
        最近更新 更多