zoukankan      html  css  js  c++  java
  • java Ajax跨域请求COOKIE无法带上的解决办法

    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
  • 相关阅读:
    正则表达式 (记录中...)
    css 坑记
    WebApi 中使用 Token
    WebApi 中使用 Session
    微信小程序 入门
    .net EF监控 MiniProfiler
    css布局
    移动端1像素边框问题
    移动端页面自适应解决方案:rem 布局篇
    js重点知识总结
  • 原文地址:https://www.cnblogs.com/q149072205/p/7676820.html
Copyright © 2011-2022 走看看