CORS跨域cookie传递

服务端

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 })

 

posted @ 2018-01-24 15:28  全玉  阅读(427)  评论(0编辑  收藏  举报