我尝试使用我的本地js文件来获取远程ip数据,但它不起作用

时间:2016-05-08 11:58:21

标签: javascript java

<script type="text/javascript">
$(function(){
    //按钮单击时执行
    alert("begin....");
    $("#testAjax").click(function(){
        $.ajax({
           type: "POST",
           url: "http://XXXXXXX:8080/iswustserver/iswust/user/queryUserList",
           data: {"page_num":"1"},
           dataType:"json",  
           contentType:'application/json;charset=utf-8',
           success: function (data) { alert("1") },  
           error: function (data) { console.log(data)}
        });
     });
});

我的服务器:

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

我使用我的本地js文件请求远程ip获取数据,但它返回400 糟糕的要求......

1 个答案:

答案 0 :(得分:0)

我知道这可能听起来很有趣,如果它不起作用,那么我们会选择其他方式:

尝试将您的类型更改为方法,例如

 $.ajax({
           method: "POST", // <--change here
           url: "http://XXXXXXX:8080/iswustserver/iswust/user/queryUserList",
           data: {"page_num":"1"},
           dataType:"json",  
           contentType:'application/json;charset=utf-8',
           success: function (data) { alert("1") },  
           error: function (data) { console.log(data)}
        });