我需要使用外部函数来成功回调,我不知道如何将json对象传递给我的函数。
$.ajax({
url:"get_box.php",
type:"POST",
data:data,
dataType:"json",
success: myFunction(data);
});
我的功能看起来像这样:
function myFunction(result2){
...
}
错误是:undefined result2 ...
答案 0 :(得分:15)
试试这种方式,
success: function(data){
myFunction(data);
});
或......
success: myFunction
});
答案 1 :(得分:1)
如何实现成功和失败回调方法(jquery documentation)。您也可以链接这些而不是在初始的ajax设置对象中提供它们,如下所示:
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?
});
答案 2 :(得分: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>