javascript - Json AJAX not working , problem in Response? -
javascript - Json AJAX not working , problem in Response? -
i have servlet application takes user input html form , extracts required info backend , makes graphs/charts , shows them user. problem seeing if user selects first alternative dropdown, works fine, info extracted backend - can see in ajax response in firebug , parsed json , maps created. info received backend (what 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 parsed in json via
function respond(req){ var res = json.parse(req.responsetext); var myarr = json.parse(res.responsestr); //forward myarr processing }
now when same user selects alternative 2, works fine, info extracted backend , can see next in 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 comes parse via
function respond(req){ var res = json.parse(req.responsetext); var myarr = json.parse(res.responsestr); //forward myarr processing
} wrong happens @ line 2 of function , hence user not see chart. if set 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 processing }
then see first alert, not second. however, first case, see both alerts. there wrong on line 2. can identify looking @ ajax reponse?
it's looking problem on server side, not on javascript side. when json.parse() tries crunch value of responsestr, looks @ first value in array, 00:00:00-01:00:00. not in quotes, it's not valid string, nor valid number, , json.parse failing. (in first example, every response value valid floating-point number, why works.)
also, you're parsing json twice, 1 time part of jquery ajax request, , 1 time again the string contained in object. though there's nil inherently wrong that, slower , can create bugs. should able encode single json string on server side, , utilize object straight in javascript.
javascript json
Comments
Post a Comment