Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript global variable not being set from ajax call
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/8187201/return-value-from-inside-of-ajax-function">Return Value from inside of $.ajax() function</a> </p> </blockquote> <p>I'm working on a CakePHP app that makes use of widespread AJAX calls to controllers. I'm stuck with one particular AJAX call in which I'm trying to assign the response from the controller to a JS global variable. Here is the code: </p> <pre class="lang-js prettyprint-override"><code>window.errors = ""; function setErrors(err) { window.errors = err; } function ajaxCall(u, t, d, dt) { var type = typeof t !== 'undefined' ? t : "post"; var dataType = typeof dt !== 'undefined' ? dt : "json"; var success = false; var err = ""; $.ajax({url: url, data: "data=" + d, type: type, dataType: dataType, success: function(d){ if(d.hasOwnProperty('success') === false) { //json response for(var i in d) { //fetch validation errors from object for(var j in i) { if(typeof d[i][j] === "undefined") { continue; } err += d[i][j] + "&lt;br/&gt;"; } } console.log(err); //&lt;=== displays correct value setErrors(err); //&lt;=== but somehow this seems to be failing?? } else { if(d.success === "1") { success = true; } } } }); return success; //&lt;=== I suspect this might be the culprit } </code></pre> <p>And this is how ajaxCall() is used:</p> <pre class="lang-js prettyprint-override"><code>function register() { var data = {}; var $inputs = $("#regForm :input"); $inputs.each(function(){ data[this.name] = $(this).val(); }); data = {"User" : data }; //CakePHP compatible object data = JSON.stringify(data); //Here's the AJAX call if(ajaxCall('https://localhost/users/add', 'post', data, 'json')) { alert("Registered!"); } else { alert(window.errors); // &lt;=== empty string console.log("window.errors is: " + window.errors); // &lt;=== empty string } } </code></pre> <p>But on the Chrome JS console, <code>window.errors</code> returns the correct value (non-empty, validation error string).</p> <p>I found a <a href="https://stackoverflow.com/questions/8960803/javascript-global-variable-not-in-scope-after-being-defined-in-callback">similar question</a> that possibly might be addressing my issue (the <code>return success</code> immediately following the <code>$.ajax()</code> is being executed before the <code>success:</code> callback). How can I fix this without drastically changing the code (also, I don't want to make this a synchronous call)?</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.
 

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