Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ajax external callback 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 this way:

function myFunction(result2){
...
}

The error is: undefined result2...

like image 953
Emil Omir Avatar asked Apr 23 '12 05:04

Emil Omir


2 Answers

Try this way,

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

or ...

success: myFunction 
    });
like image 186
Jayantha Lal Sirisena Avatar answered Oct 13 '22 11:10

Jayantha Lal Sirisena


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?
});​
like image 41
mayrs Avatar answered Oct 13 '22 12:10

mayrs