对回答有点迷惑。我从不同的JSP页面通过ajax调用调用相同的方法,但得到了不同的HTTP response。一个给200,而另一个给400。为什么会这样呢?
to JSP页面之间唯一的区别是level/depth
示例
main.jsp is at level localhost/appname/main.htm
other.jsp is at level localhost/appname/myworld/other.htm用于发布Ajax调用的URL对于两者都是相同的
function getResponse(value) {
$.ajax({
url: '${pageContext. request. contextPath}/posthere/callme.htm',
data: {
valueId: value,
},
type: "POST",
success: function (data) {
if(data == true) {
console.log("Success: ");
} else {
console.log("Failed: ");
}
}
});
}控制器
@Controller
@RequestMapping(value = "/posthere")
@SessionAttributes({"userSession"})
public class MyController {
@RequestMapping(value = "/callme", method = RequestMethod.POST)
public @ResponseBody
boolean getcalled(@RequestParam("valueId") String valueId,
@ModelAttribute("userSession") UserSession userSession,
HttpServletResponse httpServletResponse) throws IOException {
if(userSession != null) {
//do your magic. Note: This logic is not getting invoked. I have a breakpoint here.
return true;
}
return false;
}
}发布于 2013-12-19 21:23:55
检查内容类型。当内容类型是X,并且我发送的内容不等同于X时,我遇到了这个问题。
发布于 2013-12-19 21:41:02
检查在这两种情况下作为valueId传递的内容。
如果它不能被解释为String (这是不可能的)或者不存在,那么您将得到400 Bad request。
您的情况可能是第二种情况:当idValue为undefined时,它的将根本不会被发送,因此控制器接收不完整的数据-因此是400 Bad request。
https://stackoverflow.com/questions/20682837
复制相似问题