Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery cannot preserve data after .ajax() call
    primarykey
    data
    text
    <p>I am trying to do a simple call where I get data back from $.ajax() and would like to save it into a global variable which I can use in a function that called the ajax call. </p> <p>Why is the variable not being set? Why is the value that I set gets lost?</p> <pre><code>jQuery.support.cors = true; $.myNamespace = { timerID: "in namespace" }; $(document).ready(function () { $("#btnSkip").click(function () { alert($.myNamespace.timerID); startCall(); }); function startCall() { $.myNamespace.timerID = "in startCall()"; alert($.myNamespace.timerID); finishCall(); alert($.myNamespace.timerID); } function finishCall() { $.myNamespace.timerID = "in finishCall()"; alert($.myNamespace.timerID); $.ajax({ type: "GET", url: getUrl, processData: false, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { $.myNamespace.timerID = "in ajax succeeded call"; alert($.myNamespace.timerID); }, error: function (xhr, status, error) { GetSongInfoFailed(xhr, status, error); } }); } function GetSongInfoFailed(xhr, status, error) { alert('Service call failed: ' + xhr + ' ' + status + ' ' + error); } }); </code></pre> <p>Output from <code>alert()</code>s in above code:</p> <ol> <li><code>in namespace</code></li> <li><code>in startCall()</code></li> <li><code>in finishCall()</code></li> <li><code>in ajax succeeded call</code></li> <li><code>in finishCall()</code></li> </ol> <p>Why is the last variable is set to "in finishCall()" instead of "in ajax succeeded call"? </p> <p>Note, I am not a jQuery or javascript expert. </p> <p>Thank you so much!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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