CORS跨域问题。。。。。。

Java_Coder 发布于 2016/05/08 21:26
阅读 284
收藏 0

 想用本地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);
	}



ajax请求:

<!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...




加载中
0
Java_Coder
Java_Coder

自答一把。。修改后的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>




返回顶部
顶部