Click here to Skip to main content
14,667,162 members
Rate this:
Please Sign up or sign in to vote.
See more:
In the code below. When I run debugging, the sequence likes that below:
1) At var m = GetArray("VA");
2) At return s; // last line of the function
3) At debugger; // 1st debugger line
4) At debugger; // 2nd debugger line
5) Out of the function. Of course, no value is returned on var m.
How can this bug be fixed? Thanks for your advisory.

When the function GetArray() is called, it always goes to 'return s;' first in debugging
var m = GetArray("VA");
debugger;        // 1st debugger line

function GetArray(s0) {
  var s;
  $.support.cors = true;
  $.ajax({
 	async: true,
	type: 'Get',
	url: myURL,                  // myURL - a global variable 
	dataType: 'json',
	callbackParamName: 'callback',
	crossDomain: true,
	success: function (res) {
          debugger;         // 2nd debugger line
          s = res;                   // hundreds of records 
	},
	error: function (xhr, status, error) {
	  debugger;
	  console.log(error);
	}
   });
   return s;
}
Posted
Updated 19-Nov-14 4:02am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The GetArray function is returning the var s without a value as the ajax success call has not occurred yet.
When the Ajax call is successful then the 2nd debugger line is reached.

The success part of the $.ajax call can be modified like so:
...
	success: function (res) {
		AjaxSuccess(res);
	},
...
//comment out the return as the s is not being populated from the ajax call
//	return s;

function AjaxSuccess(result){
//populate the global m here, or do the processing of the result here
	m=result;
}

Hope that makes sense.
   
v2
Comments
jaket-cp 19-Nov-14 12:02pm
   
When is the m being manipulated?
I assume it is being manipulated before the m is populated with the result from the ajax call.
Also forgot to mention to remove the return s; as it is not being used as you want it to be.
Use the function AjaxSuccess to manipulate the result.
s yu 19-Nov-14 12:03pm
   
Re-coded and detected the returned value(s). Thanks a lot.
jaket-cp 19-Nov-14 12:04pm
   
no problem :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100