Jquery ajax external callback function

2019-05-05 07:03发布

I need to use an external function for my success callback and I don't know how to pass the json object to my function.

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

And my function looks this way:

function myFunction(result2){
...
}

The error is: undefined result2...

3条回答
forever°为你锁心
2楼-- · 2019-05-05 07:48
<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>
查看更多
聊天终结者
3楼-- · 2019-05-05 07:53

How about you implement both success and fail-callback methods (jquery documentation). You can also chain these instead of providing them in the initial ajax settings-object like so:

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?
});​
查看更多
趁早两清
4楼-- · 2019-05-05 07:58

Try this way,

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

or ...

success: myFunction 
    });
查看更多
登录 后发表回答