Doing my bit

User login

s.success is not a function

This post is about JavaScript, jQuery and annoying errors that don't seem to have a solution out there. Right, keywords done. Here's the problem and the solution I used;

I was having an issue with a jQuery call that was returning the following error;

s.success is not a function

The code that was generating this was the callback function on the successful return from a getJSON() call. This is the original script;

call = '/admin/ts2/pending/approve/'+sid+'/'+c1+'/'+c2+'/'+c3;
$.getJSON(call, '', 'process_approved');

After a decent amount of hunting I drew a blank. There's a reasonable number of queries about this but no answer that I could use. One was a patch on the original jQuery source but given I'm working in a framework I'm not able to touch that.

I eventually figured out a work around though. Instead of using a callback function I used an in-line function and from that called my external function. This is the code as it stands now;

call = '/admin/ts2/pending/approve/'+sid+'/'+c1+'/'+c2+'/'+c3;
$.getJSON(call, '', function(data)
  {
    process_approved(data);
  }
);

Hopefully this will be of use to someone.

Looks like you're trying to

Looks like you're trying to pass in a string instead of a function variable. In that case, s.success will be set to a string, not a function.

Instead, try taking off the quotation marks around 'process_approved':

$.getJSON(call, '', process_approved);

gurubobnz: Yeah inline

gurubobnz: Yeah inline function ftw when it comes to jQuery and JSON. Especially if this is the only plac you call your process_data() function.

The problem here is simple,

The problem here is simple, but maybe thats because I've spent so much time in javascript land...
you've wrapped your function reference in string literals. So its no longer a reference to your function its a string.

also the second argument is optional (jQuery must do some type checking on its arguments) so you can go back to the two-liner :D


var call = '..';
$.getJSON(call, process_approved);

Hope this helps.

string != function

Assuming you've been cut'n'pasting accurately from your source code, the Javascript engine was entirely correct - you passed a string containing the name of the function:

$.getJSON(call, '', 'process_approved');

instead of a reference to the function itself:

$.getJSON(call, '', process_approved);

There should be no logical difference between using that vs. using an anonymous function like you did in your workaround... except the workaround is larger and marginally slower.

[end of drive-by-debugging session]

KTHXBYE

getJSON

You might also wish to try

$.getJSON(call,'',process_approved);

ie: without the '' marks.

Assuming that function exists at the declaration/calling of that line of code it should work as prescribed.

Using string references to function names ( that is, with ' marks ) is generally not recommended.

Functions in JavaScript are "First Class" objects, which mean they can be literally passed around in variables.