AJAX调用返回:
HTTP状态404 - /mycustomproject/en/WEB-INF/views/desktop/register/region.jsp
顺便说一下,这与我用来进行Ajax调用的URL相同。
$( "#address\\.country_del" ).change(function() {
alert( "Handler for .change() called." );
$.ajax({
url: '/register/region',
data:
{
country: $("#address\\.country_del").val()
},
type: "POST",
}).done(function (data){});
});
控制器:
@RequestMapping(method = RequestMethod.POST, value = "/region")
public List<RegionData> getRegion(@RequestParam(value = "country") final String country)
{
final String isocode = country;
final List<RegionData> regions = i18NFacade.getRegionsForCountryIso(isocode);
return regions;
}
答案 0 :(得分:1)
这里的问题是你的视图解析器,因为你可以看到你的控制器类的响应被解析为jsp
HTTP状态404 - /mycustomproject/en/WEB-INF/views/desktop/register/region.jsp
因此,您需要让控制器中的方法知道它需要返回JSON
您可以通过@ResponseBody
@ResponseBody
,它的作用是以produces="application/json"
中指定的格式将对象作为正文返回。
@RequestMapping(method = RequestMethod.POST, value = "/region" ,produces="application/json")
@ResponseBody
public List<RegionData> getRegion(@RequestParam(value = "country") final String country)
{
final String isocode = country;
final List<RegionData> regions = i18NFacade.getRegionsForCountryIso(isocode);
return regions;
}