java - Retrieving JSON in HTML javascript -



java - Retrieving JSON in HTML javascript -

i using struts 1.2 in application.

what trying achieve:

1) sending ajax request using javascript action 2) returning json object response action 3) retrieving , parsing json response in javascript 4) using value.

i done first 2 steps struck 3rd 1 (retrieving json):

javascript code:

function gettransactionid(transactionid){ var xhr = new xmlhttprequest(); xhr.onreadystatechange = function(){ if(xhr.readystate == 4){ if(xhr.status == 200){ alert(xhr.responsetext); //var p = json.parse(xhr.responsetext); //alert(p); } } }; xhr.open("post", "returnmedia.do?transactionid=" + transactionid, true); xhr.send(null); }

action code:

string actualreturndatejson = new gson().tojson(actualreturndatemap); system.out.println(actualreturndatejson); response.setcontenttype("application/json"); response.setcharacterencoding("utf-8"); response.getwriter().print(actualreturndatejson);

when print json value here in console, getting right format this: {"02-19-2013":"02-19-2013"}

but when seek in javascript:

if(xhr.status == 200){ alert(xhr.responsetext); //var p = json.parse(xhr.responsetext); //alert(p); }

i not getting json value got printed before in console i.e. {"02-19-2013":"02-19-2013"}. unfortunately, alert statement showing me entire html file

please allow me know going wrong :(

thanks,

much cleaner way utilize struts2-json-plugin , allow framework lifting you

<package name="default" namespace="/" extends="json-default"> <action name="gettransactionid" class="your action class"> <result type="json" /> </action> </package>

and need not meddle response object , gson in action class,the mapping take care of it.

struts 2 , json example

java javascript servlets struts struts-1

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -