When I write a function in JSON, why do I have to enclose it inside an anonymous function?
This works:
$.ajax({
type: 'POST',
url: 'http://www.myurl.com',
data: data,
success: function(data) {
alert(data);
}
});
This doesn’t work:
$.ajax({
type: 'POST',
url: 'http://www.myurl.com',
data: data,
success: alert(data)
});
Thanks
In short, because you’re executing
alert()and trying to assign the result to thesuccesscallback, so this won’t work (the result ofalert()isundefined). However you can do this:In this case
customFuncwill receive the same parameters assuccesspasses, so it’s signature should be:customFunc(data, textStatus, XMLHttpRequest), though it can be a subset, for examplecustomFunc(data).