CORS跨域cookie傳遞

看風景就發表於2018-01-24

服務端

Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:http://a.xxx.com

客戶端

1. jquery

$.ajax({
    url: '...',
    type: 'get',
    xhrFields: {
        withCredentials: true
    },
    crossDomain: true,
    dataType: 'application/json; charset=utf-8'
});

如果是http post,ajax的請求引數中contentType不能用applicaion/json,要用application/x-www-form-urlencoded

2. vue-resource

1、全域性攔截器過濾

Vue.http.interceptors.push(function(request, next) {//攔截器
    // 跨域攜帶cookie
    request.credentials = true;
    next()
});

2、全域性選項設定

Vue.http.options.xhr = { withCredentials: true }

3、單個請求設定

this.$http.get('xxxx',{params: {},credentials: true})
this.$http.jsonp('...', { credentials: true })

 

相關文章