我有以下脚本。此脚本位于JSP中,其中一个接一个地填充了Country,State和Cities组合框(级联选择)
The script is suppose to fetch the data from the controller and populate list of states for the country selected. The function is called alert("inside ajax") is displayed and calls the controller method too. The controller fetches the list of states sucessfully and returns the list of states as "Set". when the control is passed back to the json alert("received data") doesnot pop up and nothing happens after that. There are no errors displayed too.
I have "json-lib-2.4-jdk15.jar" in my library. Do I need any more settings? or what is that I'm missing?
jason java脚本如下
<script type="text/javascript">
$(document).ready(function() {
$('#countryName').change(
function() {
alert("inside ajax");
$.getJSON('${findStateURL}', {
countryName : $(this).val(),
ajax : 'true'
}, function(data) {
alert("received data");
alert(data);
var html = '<option value="">State</option>';
var len = data.length;
alert(len);
for ( var i = 0; i < len; i++) {
html += '<option value="' + data[i].name + '">'
+ data[i].name + '</option>';
alert(html);
}
html += '</option>';
$('#states').html(html);
});
});
});
我正在调用的控制器方法是
@Controller
public class CountryController {
@Autowired
private CountryService countryService;
@RequestMapping(value = "/getStates.htm", method = RequestMethod.GET)
@ResponseBody public
Set<State> StatesForCountry(
@RequestParam(value = "countryName", required = true) String countryName, Map<String, Object> map) {
System.out.println("countryName " + countryName);
System.out.println("Country Id ");
List<State> states = countryService.getAllStates(countryName);
Set<State> state_set = new HashSet<State>(states);
System.out.println(" no. of states set = " + state_set);
return state_set;
}
控制器正在“set”中返回所需的值。但唯一的问题是我没有收到任何数据 警报(“收到的数据”);
答案 0 :(得分:0)
重新输入代码后因为我们不是机器:
$(document).ready(function() {
$('#countryName').change(
function() {
alert("inside ajax");
$.getJSON(
'${findStateURL}',
{
countryName : $(this).val(),
ajax : 'true'
},
function(data) {
alert("received data");
alert(data);
var html = '<option value="">State</option>';
var len = data.length;
alert(len);
for ( var i = 0; i < len; i++) {
html += '<option value="' + data[i].name + '">'
+ data[i].name + '</option>';
alert(html);
}
html += '</option>';
$('#states').html(html);
}
);
}
);
});
如果我读取API Doc:,如果JSON文件包含语法错误,则请求通常会无提示失败。
我建议您获取Controller返回的JSON(通过示例通过Firebug)并将您的JSON放入在线验证器中。也许你会发现错误。