jQuery ajax跨域失败 服务器是java springmvc?

新手上路,请多包涵

希望通过设置ajax请求,让ajax请求可以带上cookie信息,我按照网上说的添加

    beforeSend: function(xhr) {
            xhr.withCredentials = true;
        }

参考https://www.cnblogs.com/hello...;里面说的方法,也没办法成功,下面是我的代码,希望有过这方面经验的朋友,能够好心提供一下帮助。
js

$.ajax({
            url: URL_MANAGER+"/increase-logintime",
            type:"GET",
             dataType:"json",
            timeout : 30000,
            async:false,

            beforeSend: function(xhr) {
                xhr.withCredentials = true;
            },    
            crossDomain: true,            
            success:function(data){
                _data = data;                
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){        
            }
        })

spring mvc xml配置

    <mvc:cors>
        <mvc:mapping path="/**" allowed-origins="http://www.shanhuhaitao.com"
            allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
            allowed-headers="Content-Type, Access-Control-Allow-Headers,Access-Control-Allow-Origin, Authorization, X-Requested-With,Access-Control-Allow-Credentials"
            allow-credentials="true" />
    </mvc:cors>

请求信息

**General**
Request URL: http://localhost:8080/manager/increase-logintime
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:8080
Referrer Policy: no-referrer-when-downgrade

**response headers**
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://www.shanhuhaitao.com
Content-Type: application/json;charset=UTF-8
Date: Tue, 24 Apr 2018 01:53:28 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Vary: Origin

**request headers**
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Host: localhost:8080
Origin: http://www.shanhuhaitao.com
Referer: http://www.shanhuhaitao.com/spec-manage.html
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36

实在找不出到底哪里出了问题,希望各位前辈能提供些帮助或者解决的方法线索,万分感谢

阅读 4.1k
1 个回答

看了下页面主机是Nginx,方便的话改下配置,用反向代理的方式会比前端折腾跨域省事的多。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题