CORS跨域问题。。。。。。? 400 报错
想用本地JS加载远程服务资源,用CORS出现了问题,具体配置如下:
filter:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); chain.doFilter(req, res); } public void init(FilterConfig filterConfig) {} public void destroy() {}
@RequestMapping(value = "/queryUserList") public ResponseEntity<ResponseModelAndPageParam> getUserInfo(@RequestBody UserParam parmar) throws NotFoundException { // 检查参数有效性 if (parmar.getPage_num() < 0) { parmar.setPage_num(1); } ResponseModelAndPageParam responseModelAndPageParam = new ResponseModelAndPageParam(); PageParam pageParam = new PageParam(); List<UserResponse> list = new ArrayList<UserResponse>(); list = userservice.getUserList(parmar,pageParam); responseModelAndPageParam = ResultHelper.createOkResultAndPageParam(list,pageParam); return new ResponseEntity<ResponseModelAndPageParam>(responseModelAndPageParam, HttpStatus.OK); }
<!DOCTYPE HTML> <html> <head> <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> <script type="text/javascript"> $(function(){ //按钮单击时执行 alert("begin...."); $("#testAjax").click(function(){ $.ajax({ method: "POST", url: "http://XXXXX:8080/iswustserver/iswust/user/queryUserList", data: {"page_num":"1"}, dataType:"json", contentType:'application/json;charset=utf-8', success: function (data) { alert(data) }, error: function (data) { console.log(data)} }); }); }); </script> </head> <body> <div id="myDiv"><h2>Test</h2></div> <button id="testAjax" type="button">test</button> </body> </html>
400 bad request...
自答一把。。修改后的ajax如下:
<script type="text/javascript"> $(function(){ //按钮单击时执行 alert("begin...."); var UserParam = {}; UserParam.page_num='1'; $("#testAjax").click(function(){ $.ajax({ method: "POST", url: "http://XXXXXXX:8080/iswustserver/iswust/user/queryUserList", data:JSON.stringify(UserParam), dataType:"json", contentType:'application/json;charset=utf-8', success: function (data) { alert(data.pageParam.totalSize) }, error: function (data) { console.log(data)} }); }); }); </script>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。