Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Typical mistake. The code after the Ajax call</p> <pre><code>alert(output); return json; </code></pre> <p>is executed, <em>before</em> the Ajax call returns. It is <strong>asynchronous</strong> (meaning, it is not executed when and where you put it in the code, but at some later point in time). You can provide a callback to your function, like so:</p> <pre><code>// cb is our callback - it is a function function queryCostCenter(user_id, currency_id, country_id, cb){ var destinations = new Array(); var destination = { qty:1, country: country_id }; destinations.push(destination) var data = { destinations : $.toJSON(destinations), user_id : user_id, currency_id: currency_id }; $.ajax({ data: data, type: 'POST', url: '/lib/ajax/ajax_prepaid_cost_calculator.php', success: function(result) { // or just `success: cb` cb(result); // execute the callback with the returned data } }); } </code></pre> <p>then:</p> <pre><code>queryCostCenter(some_value, some_value, some_value, function(result) { // do something with the returned data. }); </code></pre> <p>Or put all the logic in the success handler of the Ajax call. But with a callback function, you are more flexible and you can better reuse the function.</p> <hr> <p>This a very common use case for callbacks. As you don't know <em>when</em> the Ajax call will be finished, you pass a function to the Ajax call that should be run, when some results is returned. Your are doing nothing else with the <code>success</code> handler: It is a function that is called when the call is finished. </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. 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