How to get the json result from struts2 action methode thru ajax(Using jquery) -
i have used below code display countrycode in front end onchange of country dropdown thru ajax.but not able json result action class front end. can 1 me out resolve problem ?.
struts.xml file:
<result-types> <result-type name="json" class="com.googlecode.jsonplugin.jsonresult"/> </result-types> <action name="populatecountrycode" class="com.custregnmanageraction "method="populatecountrycode"> <result name="success" type="json"> <param name="root">jsondata</param> </result> </action>
action class method:
public string populatecountrycode() throws exception { jsondata = new hashmap<string, string>(); try{ if(!getcountry().equals("") && getcountry()!=null){ list<country> countrycodenew =(list<country>) custregnmanagerservice.getcountrycode(getcountry()); for(country country: countrycodenew){ countrycode=country.getcountrycode(); jsondata.put("countrycode", countrycode); // setjsondata(jsondata); } system.out.println(jsondata); } }catch (exception e) { // todo: handle exception e.printstacktrace(); } return success; }
jquery:
$(document).ready(function() { //checks button click event $("#country").change(function(e){ var countrycode=$('#countrycode').val(); var country=$('#country').val(); $.getjson( 'populatecountrycode.action' , { country: country }, function(json) { alert("hi") ****//not displaying $('#countrycode').html(json.countrycode); countrycode=json.countrycode; } ); return false; }); });
jsp code:
<s:form action="addregistfeedback" method="post" id="theform" > <s:select headerkey="-1" headervalue="--- select ---" label="country" id="country" cssstyle="width:100%" list="countries" required="true" name="country"> </s:select><br> <table style="width: 25%"> <tr style="width: 100%"> <td ><s:label theme="simple">phone :</s:label> </td> <td style="width: 70%"> <s:textfield name="customer.countrycode"id="countrycode" cssstyle="width:20%" theme="simple" required="true" /> <s:textfield name="statecode" cssstyle="width:20%" id="statecode" theme="simple" required="true" /> <s:textfield name="citycode" cssstyle="width:45%" id="phone" theme="simple" required="true" /> </td> </tr> </table><br> <s:token name="token"></s:token> <s:submit value="save" id="button" theme="simple" cssstyle="width:50px;"></s:submit> </s:form>
please create getter setter method of map jsondata in action class.this might in resolving issue.
Comments
Post a Comment