Jquery .ajax() local variable can't assign to global - javascript

I have a jquery ajax code as following:
$(document).ready(function() {
var global_arr = new Array();
$.ajax({
url: 'result.php',
type: 'post',
dataType: 'json',
success: function(data) {
$.each(data, function(key, value) {
global_arr.push(value.name);
});
alert(global_arr); //get correct value, works fine
}
}); //end of ajax function
alert(global_arr); //get null, it doesn't work properly
});
Notice the lines to alert global_arr, why I can't get the value out of $.ajax() function?
Thanks anyone help on this.

Ajax takes time to complete. The function execution does not take nearly as much time. So by the time you get to your alert outside of the ajax request, the ajax request is still using time to complete (either in transmission or in server side actions).
You can always wait for the ajax method to be complete.
$(document).ready(function() {
var global_arr = new Array();
var complete = false;//flag to wait for ajax completion
$.ajax({
url: 'result.php',
type: 'post',
dataType: 'json',
success: function(data) {
$.each(data, function(key, value) {
global_arr.push(value.name);
});
alert(global_arr); //get correct value, works fine
complete = true;//mark ajax as complete
}
}); //end of ajax function
(function runOnComplete(){
if( complete ){//run when ajax completes and flag is true
alert(global_arr);
}else{
setTimeout(runOnComplete,25);//when ajax is not complete then loop
}
})()
});
However, the most common way is to use a callback.
$(document).ready(function() {
function runOnComplete(){//code executes once ajax request is successful
alert(global_arr);
}
var global_arr = new Array();
$.ajax({
url: 'result.php',
type: 'post',
dataType: 'json',
success: function(data) {
$.each(data, function(key, value) {
global_arr.push(value.name);
});
alert(global_arr); //get correct value, works fine
runOnComplete();//callback
}
}); //end of ajax function
});

Ajax is asynchronous. At the time the JS engine reaches your non-functioning alert() line, the AJAX call has not yet had a chance to get a response from the server and set the variable.
That's why the inner alert() works. It gets executed when the response comes in from the server.

Ajax function runs asynchronously and before ajax function adds incoming data in your array, the outer alert function runs and for this reason alerts an empty array.
You can use async-await to make the outer alert function wait for the incoming data.
$(document).ready(async function() {
var global_arr = new Array();
await $.ajax({
url: 'result.php',
type: 'post',
dataType: 'json',
success: function(data) {
$.each(data, function(key, value) {
global_arr.push(value.name);
});
alert(global_arr);
}
}); //end of ajax function
alert(global_arr); //it will work fine now
});

that is because alert(global_arr); //get null, it doesn't work properly runs before $.ajax has completed

My suggestion here would be to break this out in to 3 funcitons so it will make a bit more sense. You will need ajax, handelRequest, onComplete.
You may also want to add and error handler to your ajax function so if it does fail it can do so with out the script locking up on the user.
$(document).ready(function () {
var global_arr = new Array();
$.ajax({
url: 'result.php',
type: 'post',
dataType: 'json',
success: handelRequest(data),
error: handleError
});
function handelRequest(data) {
$.each(data, function (key, value) {
global_arr.push(value.name);
});
onComplete(global_arr); //get correct value, works fine
}
function onComplete(global_arr){
// then here you can do what ever you
// you would like with the array
alert(global_arr);
}
function handleError(){
// gracefully fail
}
})

Related

Deferred exception after populating global variable from ajax method with $.when

I have an asp.net page that loads a chart and some tables.
When the page loads I retrieve some data from a server & make a few ajax calls as shown in the code below.
I have two global variables, RainFall and RainDates that I need to populate.
When the page loads I have a function CalcSummaryStats that makes use of these global values to calculate some statistics.
My understanding (AJAX & Jquery are new to me) is that the ajax requests run asynchronously so the $.ajax method returns before the request is finished, and therefore before the success callback runs.
So after some reading I could use $.when method like the line below,
$.when(methodRainCont(), methodRainSingle()).then(calcData);
In methodRainCont the chart is draw and in this function at the same time RainDates is populated.
In methodRainSingle my other global variable is populated.
And my understanding is that this means that once both methods have finished running (assuming that includes the success callback) my function calcData is then called. calcData calls another function outside of the $(document).ready block called CalcSummaryStats & this is where my error happens.
It tries the line below
var cM = RainDates[0].getMonth();
but get this error
jQuery.Deferred exception: Cannot read property 'getMonth' of undefined TypeError: Cannot read property 'getMonth' of undefined
So it seems like RainDates is not populated? I thought using $.when would make sure both functions had run successfully before calling calcData?
My JS file
// my two global variables
var Rainfall = [];
var RainDates = [];
$(document).ready(function () {
var $opts = $('#optList');
$.ajax({
type: 'GET',
url: 'api/UserOptions',
dataType: 'json',
success: function (codes) {
$.each(codes, function (i, code) {
$opts.append('<li id="' + code + '">' + code + '</li>');
});
},
error: function () {
alert("Error ");
}
});
function methodRainCont() {
$.ajax({
type: 'GET',
url: 'api/RainContr',
dataType: 'json',
success: function (data) {
DrawChart(data);
},
error: function () {
alert("Error");
}
});
}
function methodRainSingle() {
$.ajax({
type: 'GET',
url: 'api/RainSingle',
dataType: 'json',
success: function (data) {
Rainfall = data; // setting my global variable values
},
error: function () {
alert("Error");
}
});
}
$.when(methodRainCont(), methodRainSingle()).then(calcData);
function calcData()
{
var cords = {
x: {
min: RainDates[0],
max: RainDates[RainDates.length - 1]
}
};
CalcSummaryStats(cords);
}
});
Your functions methodRainCont() and methodRainSingle() do not return a promise to be used by $.when(), so calcData() executes immediately and ajax calls are not finished.
So, inside these functions, replace$.ajax({ with return $.ajax({

Using JSON data retrieved via AJAX in separate function

Apologies if this is a duplicate question, I've followed some steps from another question which didn't seem to help me. I am trying to retrieve some JSON data, store part of the data into a variable and use that variable in a separate function outside of the AJAX request.
My expected response from the json data is http://localhost:8000/tutorials/retrieve/?page=2 (This response shows if I log the variable inside of the AJAX code) however the actual response I get when I try to log the variable from another function is as follows:
n.Event {originalEvent: MouseEvent, type: "click", timeStamp: 1436727171161, jQuery21304066238570958376: true, toElement: div#loadmore.recentTutorials…}
Here is the current code
var recentFirstPage = '';
function retrieveTutorials(){
$.ajax({
type: "GET",
url: "/tutorials/retrieve",
dataType: "json",
success: function(data){
**some unrelated parsing code here**
//Set the variable to what I need
recentFirstPage = data.next_page_url;
},
error: function() {
alert("An error occurred processing AJAX request.");
}
});
}
$('#main-content-wrap').on('click', '.recentTutorials', function(recentFirstPage){
//Does not return expected result
console.log(recentFirstPage);
});
When I click .recentTutorials I expect the console to log the data from JSON however it doesn't. Can someone help clear up my error(s)?
The reason that it doesn't log the data from JSON s that the call is asynchronous. This means that the function will execute top to bottom without waiting for the call to finish.
One method that's used is to leverage deferred objects which return a promise on completion. You can accept an anonymous function to the invoker function so that it's call back is executed within the scope of the click.
Observe:
function retrieveTutorials(){
return $.ajax({
type: "GET",
url: "/tutorials/retrieve",
dataType: "json"
});
}
$('#main-content-wrap').on('click', '.recentTutorials', function(){
//store our function call as an ajax promise
var promise = retrieveTutorials();
//wait for the ajax to return so we can mutate the data
promise.done(function(data){
//now our data will be properly
recentFirstPage = data.next_page_url;
});
});
It seems to me that you are trying to log the data before the ajax is completed. It`s better to use deferreds . Try this:
function retrieveTutorials(){
return $.ajax({ // will return deferred object
type: "GET",
url: "/tutorials/retrieve",
dataType: "json",
success: function(data){
**some unrelated parsing code here**
//Set the variable to what I need
recentFirstPage = data.next_page_url;
},
error: function() {
alert("An error occurred processing AJAX request.");
}
});
}
$.when( retrieveTutorials() ).done(function ( data ) {
console.log(recentFirstPage);
});
The parameter in your click handler is the last and final nail in your coffin. It's always the jquery event and you shouldn't handle it at all.
You do need to call the retrieveTutorials() function within the handler and you need to pass it a callback function that will be executed on success. So your retrieveTutorials() function will look something like this:
function retrieveTutorials(success){
$.ajax({ type: "GET", url: "/tutorials/retrieve",
dataType: "json",
success: success,
error: function() { alert("An error occurred processing AJAX request.");
} }); }
And your click handler:
$('#main-content-wrap').on('click', '.recentTutorials', function(){
retrieveTutorials(function(data){
console.log(data.next_page_url);
});
});
You can also use all the promise based goodness in the other anwers, but the above would be an idiom you'll see again and again.

global variable does not changed after ajax call

I am using below code
var lockonscreens = 1;
jQuery(document).ready(function(e) {
var noOfSelection = 0;
if(lockonscreens == 0){
// some stuff
}
if(lockonscreens == 1){
// some stuff
}
});
function ajaxcall(){
jQuery.ajax({
url:
type:
data:
async: false,
success: function(data){
lockonscreens = data;
}
});
}
jQuery("#").click(function(){
ajaxcall();
});
I am using above code to get some data through ajax and set it to variable and depending on that variable a click event code may happen.
But on ajax call the global variable value doesn't get changed.
It remains the same even if the data changes in ajax.
Can anyone let me know what is the issue and how to correct it?
Remember that ajax is asynchronous, so if you call ajaxCall() and next an other function, ajax start the call to server and the function end.
When the server respond, the code after success: is executed.
Make sure you call the function in the success: function

Incorporate setTimeout Into jQuery .each

The following code requests json data from a URL. Once it has that data, it runs an .each function for each element in the json data. For examples sake, I have reduced the .each function to simply doing a document.write with each value from the json and alerting "finished" after it is over.
If I remove the setTimeout, then the code works as it should, first finishing all of the .each commands and then alerting finished. However, with the setTimeout, it alerts finished before it ever executes all of the .each commands.
I have the delay itself working perfectly, however the code AFTER the initial delay executes immediately, not waiting for all of the .each commands to execute. Here is the code:
$.ajax({
type: "GET",
url: 'url to json data',
dataType: "json",
success: function(data) {
var time = 10;
$.each(data, function(key, val) {
setTimeout(function() {
document.write(val.term);
return;
}, time); time += 10;
});
alert('finished');
},
error: function () {}
})
How can I keep the delay while iterating through the .each commands and only when it is finished iterating through all of the .each commands, alert 'finished'?
What you can do is use jQuery's deferreds here. Use a promise, then call a callback when it's all done.
Untested, but you can do something like this:
$.ajax({
type: "GET",
url: 'url to json data',
dataType: "json",
success: function(data) {
var time = 10,
timeouts = [];
$.each(data, function(key, val) {
var def = new $.Deferred;
timeouts.push(def);
setTimeout(function() {
console.log(val.term);
def.resolve();
}, time);
time += 10;
});
$.when.apply($, timeouts).done(function(){
alert('finished');
});
},
error: function () {}
})
You could put the alert in a setTimeout too but reset it on each iteration so that it only runs when the each has successfully completed.
$.ajax({
type: "GET",
url: 'url to json data',
dataType: "json",
success: function(data) {
var time = 10;
var t=setTimeout(function(){alert('finished');} , 20);
$.each(data, function(key, val) {
setTimeout(function() {
//document.write(val.term);
clearTimeout(t);
t=setTimeout(function(){alert('finished');} , 20);
return;
}, time); time += 10;
});
},
error: function () {}
})

Javascript scoping query using jQuery $.ajax

I am trying to write simple function that checks to see if a designer name exists in the database. I am using jQuery's ajax function to try to do this:
function checkDesignerName(name)
{
var designer_name = $('input[name="name"]').val();
var designer_exists = false;
var temp = $.ajax( { type: "GET",
url: "/api/check_brand_exists/",
data : {name : designer_name },
success: function(data) {
designer_exists = $.parseJSON(data);
return designer_exists;
}}).statusText;
return designer_exists;
}
I have read about javascript scoping, and but still can't seem to find my bug, which is checkDesignerName always returns false. Do I need to use a closure for this function to work correctly?
Thanks
It's the nature of AJAX which is asynchronous that you seem to have troubles with understanding.
At this stage:
return designer_exists;
your AJAX call hasn't yet finished. It's only inside the success callback, which happens much later, that you can use the results. You cannot have a function which returns some result and this result depends on an AJAX call. You can only exploit the results of an AJAX call iniside the success callback:
function checkDesignerName(name)
{
var designer_name = $('input[name="name"]').val();
$.ajax({
type: "GET",
url: "/api/check_brand_exists/",
data : { name : designer_name },
success: function(data) {
var designer_exists = $.parseJSON(data);
// Here and only here you know whether this designer exists
alert(designer_exists);
}
});
}
You could of course perform a synchronous call to the server which is something totally not recommended as it will freeze the client browser and piss the user off your site during the AJAX request by setting the async: false flag:
function checkDesignerName(name)
{
var designer_name = $('input[name="name"]').val();
var designer_exists = false;
$.ajax({
type: "GET",
url: "/api/check_brand_exists/",
async: false,
data : { name : designer_name },
success: function(data) {
designer_exists = $.parseJSON(data);
}
});
return designer_exists;
}
I am mentioning this just for completeness of the answer, not as something that you should ever be doing.
Now because it seems that you are doing some kind of validation logic here, here's what I would recommend you as an ultimate solution: the jquery.validate plugin. It has this great remote rule support that will do exactly what you need here.
$.ajax is a async call. It means the statement return designer_exists gets executed even before success function is executed. That is the reason it always returns false.
your success function don't see designer_exists variable
return action runs before success function will run
You may run sync request or redesign code to callbacks logic.
For sync request your code will be:
var designer_exists = false;
function checkDesignerName(name)
{
designer_exists = false;
var designer_name = $('input[name="name"]').val();
$.ajax( { async:false,
type: "GET",
url: "/api/check_brand_exists/",
data : {name : designer_name },
success: function(data) {
designer_exists = $.parseJSON(data);
}}).statusText;
return designer_exists;
}
As Dimitrov correctly noted it's asynchronous. If you want to encapsulate the ajax call within the function you could pass in the success callback.
function checkDesignerName(name, successCallback)
and then you assign it to the jQuery ajax success function.

Categories