ajax 报400错误 ,请大神帮忙 springmvcAjax400 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 错误的请求,,你先在浏览器看看url:'/demo/cart/buy', 这个是否可以访问,然后再排查ajax语法是否有错误 别用success这个字作为键,改为别的试试。 直接访问是这个结果,应该不管用的:the server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method. 服务器拒绝服务,原因是请求格式不被支持 我没用过spring mvc 但是你这里 method = {RequestMethod.POST,RequestMethod.GET}) 又是指定post 又是get 是个什么意思?? 这个既可以接受post,又可以接受get的请求的,当时有问题,我就换了方式来回测试,都写了,也省的麻烦修改。。这个是没有问题的。。 @RequestBody Integer id 改成string id 试试吧 不太熟悉,凭直觉觉得:方法的签名不对吧?它允许带这样3个参数么?是不是应该自己从request里面取出id啊? 1、你的项目名称是不是叫做 demo?2、你的action类上有没有做 RequestMapping?如果你的项目名称叫做demo ,action 没有 RequestMapping,此时 你的 addUser 方法的注解要改成 RequestMapping(value = "/cart/buy"....如果你的项目名称不叫 demo:1、那么你的ajax请求的url那就有问题,因为根本没访问到后台,所以404;2、那么你的addUser方法要改成 RequestMapping(value = "/demo/cart/buy"....... action类上有requestMapping("cart")了,应该不是请求路径的问题,我的错误时400不是404.。。 哦,没注意,不好意思,你的spring 过滤的是什么后缀? url:'/demo/cart/buy' 需要加 http://localhost:8088/demo/cart/buy' 400这个不是服务器的错误么?应该是你启错服务器了。碰到过用myeclipse自带服务器部署项目,请求的时候没有页面出来,报400错误 但是我其他的页面跳转没有问题,就是这个ajax有问题。。 解决了,红色是和以前不一样的(先谢谢以上各位朋友解答。。): $.ajax({ type:'post', url:'/demo/cart/buy', data:{ "id": id },//没有contentType: "application/json; charset=utf-8", dataType:'json', success:function(data){ if(data.success1){ $("#cartinfo_"+id).html("购买成功"); }else{ $("#cartinfo_"+id).html("*请到购物车里修改数量"); } }, error:function(){ } });后台: @RequestMapping(value = "/buy", method = {RequestMethod.POST,RequestMethod.GET}) @ResponseBody public Map<String, String> shoppingCart(Integer id, HttpServletRequest request,HttpServletResponse response) { Map<String, String> map = new HashMap<String, String>(1); try { addToShoppingCart(id, request, response); } catch (Exception e) { map.put("success1", "false"); } map.put("success1", "true"); return map; } 对的,去掉@RequestBody 这个请求进去这个方法了。。还要去掉contentType: "application/json; charset=utf-8",这个 Calendar时间很诡异,自己会变,求教 java web 分布式系统应用 javabean问题 用eclipse挂websphere,怎么配置和使用啊? 请教高手一些OpenCms的使用问题 求救,任何写个注册页面,javabean 关于hibernate中保存Blob数据的问题 帮忙看看我的JavaScript为什么没有作用,是不是哪错了。 用resin能不能使用EJB? weblogic 連接池的問題 Swing刷新界面 用.do调用action,出现问题,求前辈指教
直接访问是这个结果,应该不管用的:the server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.
服务器拒绝服务,原因是请求格式不被支持
它允许带这样3个参数么?是不是应该自己从request里面取出id啊?
2、你的action类上有没有做 RequestMapping?
如果你的项目名称叫做demo ,action 没有 RequestMapping,
此时 你的 addUser 方法的注解要改成 RequestMapping(value = "/cart/buy"....如果你的项目名称不叫 demo:
1、那么你的ajax请求的url那就有问题,因为根本没访问到后台,所以404;
2、那么你的addUser方法要改成 RequestMapping(value = "/demo/cart/buy".......
type:'post',
url:'/demo/cart/buy',
data:{ "id": id },//没有contentType: "application/json; charset=utf-8",
dataType:'json',
success:function(data){
if(data.success1){
$("#cartinfo_"+id).html("购买成功");
}else{
$("#cartinfo_"+id).html("*请到购物车里修改数量");
}
},
error:function(){
}
});后台: @RequestMapping(value = "/buy", method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Map<String, String> shoppingCart(Integer id, HttpServletRequest request,HttpServletResponse response) {
Map<String, String> map = new HashMap<String, String>(1);
try {
addToShoppingCart(id, request, response);
} catch (Exception e) {
map.put("success1", "false");
}
map.put("success1", "true");
return map;
}