我需要为我的成功回调使用外部函数,但我不知道如何将 json 对象传递给我的函数。
$.ajax({
url:"get_box.php",
type:"POST",
data:data,
dataType:"json",
success: myFunction(data);
});
我的函数看起来是这样的:
function myFunction(result2){
...
}
错误是:未定义的结果2 ...
试试这个方法
success: function(data){
myFunction(data);
});
或者 ...
success: myFunction
});
您如何同时实现成功和失败回调方法(jquery 文档)。您还可以链接这些而不是在初始 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?
});
<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>