Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've made a couple of different attempts to get the data here, and they fail for different reasons. I'll explain why in a moment, first though — the solution:</p> <p><strong>Whatever you do with the data has to be done in the callback function.</strong></p> <p>Now, why your attempts don't work:</p> <p>You can't return data from an Ajax request.</p> <p>Ajax works (usually) by creating an XHR object, setting an event handler for "When the request comes back", and then sending an HTTP request.</p> <p>You can't return data from it for the same reason that you can't get the event object returned from:</p> <pre><code>var not_the_event = jQuery('foo').click(function (event) { return event; }); </code></pre> <p>You can't set the value of a variable called <code>thumb</code> and defined outside a function from inside a function that defines <code>thumb</code> using the <code>var</code> keyword. The <code>var</code> keyword creates a new instance of the variable that is scoped to the function.</p> <p>If you took the <code>var</code> away from the callback function, it still wouldn't work because you would have timing issues. </p> <p>If you had:</p> <pre><code>var not_the_event; jQuery('foo').click(function (event) { not_the_event = event; }); alert(not_the_event); </code></pre> <p>Then you would be alerting <code>undefined</code> because the click hasn't taken place when the <code>alert</code> is called, so the variable hasn't been set by the event handling function.</p> <p>With Ajax, the HTTP request hasn't been answered by the time the alert is reached, so it is also undefined.</p> <p>Which brings us back to: Whatever you do with the data has to be done in the callback function.</p> <p>The point of the callback function is that it is called when the data is ready. So whatever you do with the data can be done then.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload