码迷,mamicode.com
首页 > 编程语言 > 详细

java Ajax跨域请求COOKIE无法带上的解决办法

时间:2017-10-16 16:53:55      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:pattern   new   type   style   test   一个   app   att   post方法   

1.web.xml加入以下节点,,一定放在第一个filter

<!--目录下所有文件可以跨域Begin-->
<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

2.ajax添加参数

 

原生ajax请求方式:
var xhr = new XMLHttpRequest();  
xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);  
xhr.withCredentials = true; //支持跨域发送cookies
xhr.send();

jquery的ajax的post方法请求:
 $.ajax({
               type: "POST",
               url: "http://xxx.com/api/test",
               dataType: ‘jsonp‘,
               xhrFields: {
                      withCredentials: true
              },
            crossDomain: true,
           success:function(){
     },
           error:function(){
    }
})

这二个参数都要加:
 xhrFields: {withCredentials: true},
 crossDomain: true,


另外的一个问题:
js跨域调用,Iframe子页跨域调父页的js方法
在子页和父页设置document.domain = ‘abc.com‘;//本地就用locahost

java Ajax跨域请求COOKIE无法带上的解决办法

标签:pattern   new   type   style   test   一个   app   att   post方法   

原文地址:http://www.cnblogs.com/q149072205/p/7676820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!