Heim  >  Artikel  >  Backend-Entwicklung  >  javascript - 为何 二级域名下cookie可以获取,却不能使用ajax自动发回,cookie 不能跨域?

javascript - 为何 二级域名下cookie可以获取,却不能使用ajax自动发回,cookie 不能跨域?

WBOY
WBOYOriginal
2016-08-18 09:16:14956Durchsuche

情景复现流程:

<code>机器A局域网地址:192.168.9.8
机器B局域网地址:192.168.9.9
</code>
<code>分别配置机器A、B的hosts地址:

配置机器A的地址:192.168.9.8 --> a.site.com 
配置机器B的地址:192.168.9.9 --> b.site.com
</code>

机器A为前端js代码 ,启动了服务,并在浏览器上请求 机器B(代表服务器端)
的第一个地址http://b.site.com:8080/login/... , 请求的同时 服务器端 注入了cookie,
设置 cookie 的key为‘site’,value为‘date’,doman 为 .site.com, path 为 /
,然后在浏览器上开新页面并在新页面请求 http://a.site.com:8080/index , 通过chrome控制台可以看到
http://b.site.com:8080/
注入的cookie,但是.......通过ajax走接口的时候,cookie并没有出现在请求头,试了多种方式,get、post、异步、同步都不行。最后使用了jsonp的方式,cookie出现了


不好用的代码如下:

<code>$("a").click(function(event) {
        $.ajax({
            url: 'http://b.site.com:8080/test/test.htm',
            type: 'post',
            dataType: 'json',
            data: {'token': 'value'},
        })
        .done(function() {
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
});
</code>

好用的代码如下:

<code>function flightHandler(data) {
    console.log(data);
    // body...
};

$("a").click(function(event) {
    $.ajax({
         type: "get",
         url: "http://b.site.com:8080/test/test.htm",
         dataType: "jsonp",
         jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
         jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
         success: function(data){
            console.log(data);
         },
         error: function(){
             alert('fail');
         }
     });
});
</code>

请大神们帮忙看看,如能解惑,万分感谢

回复内容:

情景复现流程:

<code>机器A局域网地址:192.168.9.8
机器B局域网地址:192.168.9.9
</code>
<code>分别配置机器A、B的hosts地址:

配置机器A的地址:192.168.9.8 --> a.site.com 
配置机器B的地址:192.168.9.9 --> b.site.com
</code>

机器A为前端js代码 ,启动了服务,并在浏览器上请求 机器B(代表服务器端)
的第一个地址http://b.site.com:8080/login/... , 请求的同时 服务器端 注入了cookie,
设置 cookie 的key为‘site’,value为‘date’,doman 为 .site.com, path 为 /
,然后在浏览器上开新页面并在新页面请求 http://a.site.com:8080/index , 通过chrome控制台可以看到
http://b.site.com:8080/
注入的cookie,但是.......通过ajax走接口的时候,cookie并没有出现在请求头,试了多种方式,get、post、异步、同步都不行。最后使用了jsonp的方式,cookie出现了


不好用的代码如下:

<code>$("a").click(function(event) {
        $.ajax({
            url: 'http://b.site.com:8080/test/test.htm',
            type: 'post',
            dataType: 'json',
            data: {'token': 'value'},
        })
        .done(function() {
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
});
</code>

好用的代码如下:

<code>function flightHandler(data) {
    console.log(data);
    // body...
};

$("a").click(function(event) {
    $.ajax({
         type: "get",
         url: "http://b.site.com:8080/test/test.htm",
         dataType: "jsonp",
         jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
         jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
         success: function(data){
            console.log(data);
         },
         error: function(){
             alert('fail');
         }
     });
});
</code>

请大神们帮忙看看,如能解惑,万分感谢

withCredentials=true,并且保证服务端允许跨域,具体可以参考cors

domain不是你说是什么就是什么。cookie的domain和ajax的domain不是一回事

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn