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...
Try this way,
success: function(data){
myFunction(data);
});
or ...
success: myFunction
});
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?
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With