3

I'm confused about the "clean" way to call $.getJSON.

According to the documentation, the synopsis is:

jQuery.getJSON( url [, data ] [, success ] )

where data is "a plain object or string that is sent to the server with the request", and success is "a callback function that is executed if the request succeeds".

In my experience, the data argument is often not needed in practice, in which case I call $.getJSON as

$.getJSON( some_url, {}, function(response) { /* do something */ });

That is, I simply pass an empty object as data. However, I've also seen people use

$.getJSON( some_url, function(response) { /* do something */ });

This is confusing, since it looks like the callback function is being passed as the data object. Yet it seems to work just fine.

How come this works? Is jQuery "clever" enough to understand the second syntax, even though it doesn't strictly correspond to the specification? Is there effectively some difference in what happens in those two calls? Between the two, is there a preferred way?

Malte Skoruppa
  • 1,232
  • 1
  • 19
  • 25
  • 1
    The `[ ]` around the parameter in the docs means it's optional. And JavaScript is not strict about the number and type of arguments, so you can (or have to) check the number and types of your arguments to see how it's being called (i.e. yes, jQuery is "clever enough" to understand). – ajp15243 Jan 06 '15 at 16:49
  • @ajp15243: Please write answers as _answers_; this ain't a chatroom! Thanks. – Lightness Races in Orbit Jan 06 '15 at 16:51

2 Answers2

4

Is jQuery "clever" enough to understand the second syntax,

Yes; there is code inside getJSON to detect the types of its arguments and sort it out that way.

even though it doesn't strictly correspond to the specification

It does. The synopsis you quoted explicitly says exactly that! Square brackets ([]) in synopses indicate optional arguments. In this case, both arguments are independently optional: you may provide either, or both, or neither.

Is there effectively some difference in what happens in those two calls?

No.

Between the two, is there a preferred way?

No.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • The gist of the question has to do with argument type detection. Do you have more to say about that? – isherwood Jan 06 '15 at 16:52
  • OP is free to examine the jQuery code if he wishes to learn precisely how it works. – Lightness Races in Orbit Jan 06 '15 at 16:53
  • I just did so, and you're right of course. I guess I read the synopsis wrong - i.e., as `jQuery.getJSON( url [, data [, success ] ] )` instead of (correctly) `jQuery.getJSON( url [, data ] [, success ] )`. It would be interesting if you added something about argument type detection, but to be fair I didn't ask about it. :-) Thanks! – Malte Skoruppa Jan 06 '15 at 16:57
  • @MalteSkoruppa: Ah, I see. Easy mistake to make, then! – Lightness Races in Orbit Jan 06 '15 at 16:58
3

The best way I have found to use $.getJSON without any parameters is like this

$.getJSON('path/to/json.json')
    .done(function(response) {
        console.log(response);
    });

Using deferred's is preferable to using callbacks in almost all circumstances.

This question is a particular favorite of mine to explain why you should choose the deferred syntax -> Asynchronous JavaScript - Callbacks vs Deferred/Promise

Community
  • 1
  • 1
Undefined
  • 11,234
  • 5
  • 37
  • 62