【发布时间】:2014-07-26 18:55:37
【问题描述】:
我的 spring 控制器包含这样的 get 处理程序:
@RequestMapping(value = "/country", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Region> getRegionsFor(@RequestParam(value = "countryName") String countryName,
@RequestParam(value = "geonameId") Long geonameId) {
logger.debug("fetching regions for {}, with geonameId {}", countryName, geonameId);
RegionInfo regionInfo = restTemplate.getForObject(
"http://api.geonames.org/childrenJSON?geonameId={geonameId}&username=geonameUser2014",
RegionInfo.class, geonameId);
return regionInfo.getRegions();
}
@Controller 映射到/hostel。所以网址是/hostel/country?countryName=%27&Albania%27&&geonameId=783754
当我在 chrome 浏览器中输入时
http://localhost:8080/HostMe/hostel/country?countryName=%27Albania%27&geonameId=783754
它按预期返回 json 响应!!!
但我想通过以下使用 jquery 进行的 ajax 调用来访问此 url:
$.ajax({
headers : {
'Accept' : 'application/json'
},
url : '/hostel/country',
dataType : 'json',
data : {countryName:"Albania",geonameId:783754},
type : 'GET',
async : true,
success : function(response) {
console.log("response=" + response.join(','));
},
error : function(errorData) {
console.log("data on fail ");
printObject(errorData);
}
});
你猜这根本行不通。 Http 状态 404 (Not Found) 返回到error: handler。
我该如何解决这个问题?
【问题讨论】:
标签: java jquery ajax spring-mvc