I currently use a annonymous function for the jquery ajax success-response.
success: function(data){
code
},
When Trying to use a named function:
success: success(data),
It claims the the variable data is not set. I suppose i could do
success: function(data){
success(data)
},
But this feels like it’s not how it’s suppose to be done. Why is it behaving this way and what should I do about it?
It’s because JQuery is expecting a reference to a function, not a function call.
JQuery needs to know what function has to call. With
success(data)you are instead passing the result of the function callsuccess(data)(which can be whatever, also void – no result) giving no clue at all for what to do.The reference is something that points to an object, in this case a “function object”.
The function call can be thought instead as an action to perform that gives or gives not a result that is evaluated as an expression.
Just use: