2012-04-23 101 views
3

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

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

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

function myFunction(result2){ 
... 
} 

的错误是:不确定RESULT2 ...

回答

11

尝试这种方式,

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

或...

success: myFunction 
    }); 
1

您如何实施成功和失败回调方法(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? 
});​ 
0
<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>