kaykay
kaykay

Reputation: 11

Json AJAX not working , problem in Response?

I have a servlet application that takes the user input from HTML form , extracts the required data from backend and makes graphs/charts and shows them to user. The problem I am seeing is that if user selects first option from dropdown, everything works fine, the data is extracted from backend - I can see it in the AJAX response in firebug and then its parsed by json and then maps are created. The data that is received from backend is (what I see in AJAX response):

{"responseStr":"[47.636597,-122.189495,0,1,47.643647,-122.212038,0,26,47.505288,-122.339112,0,1,47.622741,-122.314592,0,60,47.541612,-122.129318,0,1,47.568435,-122.161237,0,166,47.682308,-122.196004,0,2,47.666673,-122.284099,0,1,47.612953,-122.316700,0,2,47.600605,-122.322286,0,30,47.589557,-122.315608,0,27,47.636351,-122.327213,0,1,47.630270,-122.177084,2,0,47.630432,-122.140126,17,0,47.621644,-122.132080,1,3,47.630808,-122.153539,86,75,47.622367,-122.337023,495,3466,47.630886,-122.306255,1423,45,47.720287,-122.090885,255,82,47.702376,-122.093340,47,4,47.676897,-122.318752,1,0,47.760994,-122.322550,1,2,47.588854,-122.221273,1,0,39.530179,-119.818395,1,1,47.631306,-122.342762,1,0,47.737242,-122.323710,1,0,47.747054,-122.305083,2,0,47.752018,-122.316452,1,0]"}

This is then parsed in json via

function Respond(REQ){
  var res = JSON.parse(REQ.responseText);
  var myArr = JSON.parse(res.responseStr);
  //forward myArr for processing
}

Now when the same user selects option 2, all works fine, the data is extracted from backend and I can see the following in the response

{"responseStr":"[00:00:00-01:00:00,100,30,0,01:00:00-02:00:00,100,29,0,02:00:00-03:00:00,100,34,0,03:00:00-04:00:00,100,5,0,04:00:00-05:00:00,100,7,0,05:00:00-06:00:00,100,23,0,06:00:00-07:00:00,78,29,0,07:00:00-08:00:00,48,17,0,08:00:00-09:00:00,24,35,0,09:00:00-10:00:00,18,29,0,10:00:00-11:00:00,5,28,0,11:00:00-12:00:00,45,57,0,12:00:00-13:00:00,65,69,0,13:00:00-14:00:00,64,58,0,14:00:00-15:00:00,73,46,0,15:00:00-16:00:00,72,27,0,16:00:00-17:00:00,94,9,0,17:00:00-18:00:00,69,15,0,18:00:00-19:00:00,14,9,0,19:00:00-20:00:00,25,13,0,20:00:00-21:00:00,81,38,0,21:00:00-22:00:00,53,74,0,22:00:00-23:00:00,76,55,0,23:00:00-24:00:00,89,16,0]"}

but when it comes to parse this via

function Respond(REQ){
   var res = JSON.parse(REQ.responseText);       
   var myArr = JSON.parse(res.responseStr);
   //forward myArr for processing

} something wrong happens at line 2 of the function and hence user does not see a chart. If I were to put alerts in Respond function,

function Respond(REQ){
   var res = JSON.parse(REQ.responseText);       
   alert('here');
   var myArr = JSON.parse(res.responseStr);
   alert('here2');
   //forward myArr for processing
}

then I do see first alert, but not the second. However, for the first case, i see both the alerts. So there is definitely something wrong on line 2. Can someone identify this by looking at the AJAX reponse?

Upvotes: 0

Views: 168

Answers (2)

kaykay
kaykay

Reputation: 11

Allrit, so in order to make the above a valid string, each of the 00:00:00-01:00:00 (similar ) strings should be enclosed in double quotes. So at a place in my servlet that was fetching this data from database, earlier I was doing

resultSet.getString(1); 

now I changed it to

"\"" +  resultSet.getString(1)+ "\"";

and it works.

Upvotes: 1

Luke Dennis
Luke Dennis

Reputation: 14550

It's looking like this is a problem on the server side, not on the Javascript side. When JSON.parse() tries to crunch the value of responseStr, it looks at the first value in the array, which is 00:00:00-01:00:00. As this not in quotes, it's not a valid string, nor is it a valid number, and so JSON.parse is failing. (In the first example, every response value is a valid floating-point number, which is why it works.)

Also, you're currently parsing JSON twice, once as part of the jQuery ajax request, and again with the the string contained in that object. Though there's nothing inherently wrong with that, it is slower and can create bugs. You should be able to encode everything into a single JSON string on the server side, and then just use the object directly in your Javascript.

Upvotes: 1

Related Questions