【问题标题】:Jquery ajax external callback functionjQuery ajax 外部回调函数
【发布时间】:2012-05-03 18:07:59
【问题描述】:

我需要为我的成功回调使用外部函数,但我不知道如何将 json 对象传递给我的函数。

$.ajax({
url:"get_box.php",
type:"POST",
data:data,
dataType:"json",
success: myFunction(data);  
    });

我的函数看起来是这样的:

function myFunction(result2){
...
}

错误是:未定义的result2...

【问题讨论】:

    标签: jquery ajax json function callback


    【解决方案1】:

    试试这个方法,

     success: function(data){
            myFunction(data);
        });
    

    或者...

    success: myFunction 
        });
    

    【讨论】:

      【解决方案2】:

      如何实现成功和失败回调方法 (jquery documentation)。您也可以将它们链接起来,而不是像这样在初始 ajax 设置对象中提供它们:

      Here is a fiddle

      jQuery.ajax({
          // basic settings
      }).done(function(response) {
          // do something when the request is resolved
          myFunction(response);
      }).fail(function(jqXHR, textStatus) {
          // when it fails you might want to set a default value or whatever?
      }).always(function() {
          // maybe there is something you always want to do?
      });​
      

      【讨论】:

        【解决方案3】:
        <script>
        function fun(){
            $.ajax({
                url : "http://cdacmumbai.in/Server.jsp?out=json&callback=?",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                type: "GET",
                success: function ( output ) {
                    var data = eval( output );
                    document.getElementById("datetime").innerHTML = "Server Date&Time: "+data.servertime;
                    document.getElementById("hostname").innerHTML = "Server Hostname: "+data.hostname;
                    document.getElementById("serverip").innerHTML = "Server IP Address: "+data.serverip;
                    }
                });
              }
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-19
          • 1970-01-01
          • 1970-01-01
          • 2013-01-23
          相关资源
          最近更新 更多