5

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...

Emil Omir
  • 87
  • 1
  • 6

3 Answers3

17

Try this way,

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

or ...

success: myFunction 
    });
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
1

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?
});​
mayrs
  • 2,299
  • 2
  • 24
  • 35
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>
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335