ajax 报400错误 ,请大神帮忙

明亮哥哥 2013-08-18 07:50:50

首先报的错误时:



前台:
function buy(id){

$.ajax({
type:'post',
url:'/demo/cart/buy',
data:{ "id": id },
contentType: "application/json; charset=utf-8",
dataType:'json',
success:function(data){
if(data.success){
$("#cartinfo_"+id).html("购买成功");
}else{
$("#cartinfo_"+id).html("购买失败");
}
},
error:function(){

}
});

后台
@RequestMapping(value = "/buy", method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public Map<String, String> addUser(@RequestBody 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("success", "false");
}
map.put("success", "true");
return map;
}

求大神指点迷津呀呀呀。。
...全文
15979 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
joeklaus 2014-10-21
  • 打赏
  • 举报
回复
7楼也能解决
yujian10962 2013-08-20
  • 打赏
  • 举报
回复
上一条作废!!
yujian10962 2013-08-20
  • 打赏
  • 举报
回复
web.xml文件中有相关配置么
明亮哥哥 2013-08-19
  • 打赏
  • 举报
回复
引用 7 楼 xiuxiu2by 的回复:
@RequestBody Integer id 改成string id 试试吧
对的,去掉@RequestBody 这个请求进去这个方法了。。。。。还要去掉contentType: "application/json; charset=utf-8",这个
明亮哥哥 2013-08-19
  • 打赏
  • 举报
回复
解决了,红色是和以前不一样的(先谢谢以上各位朋友解答。。): $.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; }
抱抱枯叶 2013-08-19
  • 打赏
  • 举报
回复
@RequestBody Integer id 改成string id 试试吧
明亮哥哥 2013-08-19
  • 打赏
  • 举报
回复
引用 5 楼 ch656409110 的回复:
我没用过spring mvc 但是你这里 method = {RequestMethod.POST,RequestMethod.GET}) 又是指定post 又是get 是个什么意思??
这个既可以接受post,又可以接受get的请求的,当时有问题,我就换了方式来回测试,都写了,也省的麻烦修改。。这个是没有问题的。。
明亮哥哥 2013-08-19
  • 打赏
  • 举报
回复
引用 14 楼 yujian10962 的回复:
400这个不是服务器的错误么?应该是你启错服务器了。碰到过用myeclipse自带服务器部署项目,请求的时候没有页面出来,报400错误
但是我其他的页面跳转没有问题,就是这个ajax有问题。。
yujian10962 2013-08-19
  • 打赏
  • 举报
回复
400这个不是服务器的错误么?应该是你启错服务器了。碰到过用myeclipse自带服务器部署项目,请求的时候没有页面出来,报400错误
明亮哥哥 2013-08-19
  • 打赏
  • 举报
回复
引用 11 楼 zcwgogo 的回复:
哦,没注意,不好意思,你的spring 过滤的是什么后缀?
没事的,过滤所有的 /*
chuxingzhe 2013-08-19
  • 打赏
  • 举报
回复
url:'/demo/cart/buy' 需要加 http://localhost:8088/demo/cart/buy'
zcwgogo 2013-08-19
  • 打赏
  • 举报
回复
哦,没注意,不好意思,你的spring 过滤的是什么后缀?
明亮哥哥 2013-08-19
  • 打赏
  • 举报
回复
引用 9 楼 zcwgogo 的回复:
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.。。
zcwgogo 2013-08-19
  • 打赏
  • 举报
回复
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".......
owen1759 2013-08-19
  • 打赏
  • 举报
回复
不太熟悉,凭直觉觉得:方法的签名不对吧? 它允许带这样3个参数么?是不是应该自己从request里面取出id啊?
  • 打赏
  • 举报
回复
我没用过spring mvc 但是你这里 method = {RequestMethod.POST,RequestMethod.GET}) 又是指定post 又是get 是个什么意思??
明亮哥哥 2013-08-18
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
别用success这个字作为键,改为别的试试。
别的也不行的。。。
明亮哥哥 2013-08-18
  • 打赏
  • 举报
回复
引用 1 楼 ch656409110 的回复:
错误的请求,,你先在浏览器看看url:'/demo/cart/buy', 这个是否可以访问,然后再排查ajax语法是否有错误。。。
直接访问是这个结果,应该不管用的: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. 服务器拒绝服务,原因是请求格式不被支持
  • 打赏
  • 举报
回复
别用success这个字作为键,改为别的试试。
  • 打赏
  • 举报
回复
错误的请求,,你先在浏览器看看url:'/demo/cart/buy', 这个是否可以访问,然后再排查ajax语法是否有错误。。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧