我已经使用下面的代码在前端通过 ajax 从下拉列表中显示国家代码更改。但是我无法从操作类到前端获取 json 结果。任何人都可以帮我解决这个问题吗?
Struts.xml 文件:
<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>
动作类方法:
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;
}
查询:
$(document).ready(function() {
//checks for the 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代码:
<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>