CORS跨域问题。。。。。。
想用本地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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自答一把。。修改后的ajax如下: