gpt4 book ai didi

java - jquery ajax GET 上的 HTTP 状态 404(未找到)到 Spring Controller ?

转载 作者:行者123 更新时间:2023-11-30 03:48:34 25 4
gpt4 key购买 nike

我的 spring Controller 包含这样的 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(未找到)返回到 error: handler 。

我该如何解决这个问题?

最佳答案

ajax 调用中的 url 是相对于主机名的。您需要添加您的网络应用程序上下文

url : '/HostMe/hostel/country',

关于java - jquery ajax GET 上的 HTTP 状态 404(未找到)到 Spring Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24974451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com