JQuery ajax external call function

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 like this:

function myFunction(result2){
...
}

Error: undefined result2 ...

+3
source share
3 answers

Try this way

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

or...

success: myFunction 
    });
+11
source

How about how you implement successful and unsuccessful callback methods ( jquery documentation ). You can also link them, rather than providing them in the original ajax configuration object:

Here is the 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?
});​
+1
source
<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>
0
source

All Articles