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

3 Answers 3

17

Try this way,

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

or ...

success: myFunction 
    });
Sign up to request clarification or add additional context in comments.

Comments

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?
});​

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.