开发者社区> 问答> 正文

CORS跨域问题。。。。。。? 400 报错

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);
	}



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




展开
收起
爱吃鱼的程序员 2020-06-03 15:42:40 609 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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




    2020-06-03 20:53:38
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载