Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Callback functions, like the success handler, are registered, asynchronous events that fire once the AJAX request is completed and returns successful results to the client browser. Since the event is registered, it doesn't block the function that you're AJAX request is in from running.</p> <p>In order to process the data, simply hand off the data to another function, like so:</p> <pre><code>$.ajax({ url : 'curriculum/read_types', type : 'GET', dataType : 'JSON', success : function(data) { console.log(data): // process the results processData(data); } }); function processData(data) { // do stuff with the data here } </code></pre> <p><strong>UPDATE:</strong></p> <pre><code>read_types : function() { $.getJSON('curriculum/read_types', function(data) { return data; }); } </code></pre> <p>The above code is simply something you cannot do. Here is a loose description of the flow:</p> <blockquote> <ul> <li><p><code>read_types</code> function is called from some other process.</p></li> <li><p>The $.getJSON function is called with 2 arguments: path and callback handler.</p></li> <li><p>The read_types function finishes processing and reaches the end.</p></li> <li>Next, while the read_types method is in the process of finishing up, the getJSON function makes an HTTP GET request to your URL.</li> <li>The data is received in the response and passed to the callback handler as an argument assigned to the parameter "data".</li> <li>When you call <code>return data;</code> you are returning data to the anonymous success callback function, not read_types. Thus, the return statement essentially does nothing.</li> </ul> </blockquote> <h2><strong>Synchronous Request Example:</strong></h2> <p>Now, with that said, you can make synchronous requests to the server, but this is strongly discouraged because of the effects it has on the view.</p> <p>However, here is an example, for academic purposes only. I would never advocate for using this strategy unless you really really really know what you are doing:</p> <p><strong>NOTE: This is not to be used in production unless you know what you're doing!</strong></p> <pre><code>function getData() { var myDataObj = null; // this will BLOCK execution of all other scripts on the page until // the data is received! $.ajax({ url : 'curriculum/read_types', type : 'GET', dataType : 'JSON', success : function(data) { console.log(data): // we can assign this data to myDataObj in synchronous requests myDataObj = data; }, async: false /** Make a synchronous request **/ }); // Since we BLOCKED, this object is not null. If we used async: true // (the default) then this would return null. return myDataObj; } </code></pre>
    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. 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