Spring MVC Json 405 (Method Not Allowed)/ 404 Bad Request
来源:互联网 发布:pr cc 2015 mac 下载 编辑:程序博客网 时间:2024/05/15 23:02
在spring mvc 框架下利用Ajax向后台发送Json格式数据,出现了404、405问题。
原因如下:
- 出现404,是因为Ajax的type设成了“GET”,但是@RequestBody注解不支持GET,只支持POST.
- 出现405,是因为框架添加了spring security, 凡是post方法都需要token验证。
具体配置如下:
ajax:
<script type="text/javascript"> var value=[]; $(document).ready(function() { var token = $('#csrfToken').val(); var header = $('#csrfHeader').val(); var saveDataAry=[]; var data1={username:"test",email:"gz"}; var data2={username:"ququ",email:"gr"}; saveDataAry.push(data1); saveDataAry.push(data2); $("#click").click(function(){ var link=$("#urlpath").val(); $.ajax( { url:link, type:"POST", dataType:"json", contentType:"application/json", data:JSON.stringify(saveDataAry), beforeSend: function(xhr) { xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader(header, token); } }); }); }); </script>
<body><input type="hidden" id="csrfToken" value="${_csrf.token}"/><input type="hidden" id="csrfHeader" value="${_csrf.headerName}"/><input type="hidden" id="urlpath" value=<c:url value="/auth/json"/>></div> <button id="click">Click</button></body>
Controller
@RequestMapping(value="/auth/json",method=RequestMethod.POST,consumes="application/json") public @ResponseBody String getShopInJSON(@RequestBody List<TestUser>user) { System.out.println(user.get(0).getEmail()); return ""; }
TestUser
package com.user;public class TestUser { private String username; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }}
0 0
- Spring MVC Json 405 (Method Not Allowed)/ 404 Bad Request
- Spring MVC出现POST 400 Bad Request &405 Request method 'GET' not supported
- HTTP Status 405 - Request method 'POST' not supported (Spring MVC)
- Spring mvc 405 Request method 'POST' not support
- Spring MVC-Request method 'POST' not supported:
- Method Not Allowed (#405) Method Not Allowed. This url can only handle the following request methods
- spring mvc处理json文件静态资源文件报错Request method 'POST' not supported
- 用spring mvc+mybaties写用户信息修改出现HTTP 405 错误--Method not allowed
- SVN报MKCOL request for ... 405 Method Not Allowed错误
- 405 Method Not Allowed
- 405 Method Not Allowed
- 405 (Method Not Allowed)
- 405 Method Not Allowed
- Method Not Allowed 405
- Spring MVC Request method 'POST' not supported错误
- jsp spring mvc 上传时HTTP Status 405 - Request method 'GET' not supported是什么原因
- Spring MVC HTTP Status 405 – Request method ‘POST’ not supported
- iis 405 Method Not Allowed
- 红帽6 RHCE&RHCS认证培训笔记(5)
- FatMouse' Trade
- Criteria 和 DetachedCriteria的区别与使用
- 网页编码的问题
- 浏览器内核渲染引擎工作流程
- Spring MVC Json 405 (Method Not Allowed)/ 404 Bad Request
- oracle计算连续登陆/上班天数
- FileChannel、ByteBuffer对文件操作过程对比
- 博文收藏
- float使用
- 给程序员的建议
- Binary Tree Level Order Traversal II
- ContextMenu上下文菜单 PopupMenu弹出式菜单 小例子
- spring之BeanFactoryAware接口