java Ajax跨域請求COOKIE無法帶上的解決辦法
阿新 • • 發佈:2017-10-16
coo $.ajax test style dem log json bsp ros
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無法帶上的解決辦法