JavaScript-why are cookies available under a two-level domain name, but not automatically sent back using Ajax, cookies cannot cross domains?

Source: Internet
Author: User
Scenario Recurrence Process:

机器A局域网地址:192.168.9.8机器B局域网地址:192.168.9.9
分别配置机器A、B的hosts地址:配置机器A的地址:192.168.9.8 --> a.site.com 配置机器B的地址:192.168.9.9 --> b.site.com

Machine A is the front-end JS code, starts the service, and requests machine B on the browser (on behalf of the server side)
The first address of the http://b.site.com:8080/login/..., the request of the server side injected a cookie,
The key for setting the cookie is ' site ', value is ' date ', Doman is. site.com, path is/
, then open a new page on the browser and request Http://a.site.com:8080/index on the new page, which you can see through the chrome console
http://b.site.com:8080/
injected cookies, but ..... Through the Ajax interface, the cookie does not appear in the request header, tried a variety of ways, get, post, asynchronous, synchronization is not. Finally, using the Jsonp method, the cookie appears

The bad code is as follows:

$("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");        });});

The useful code is as follows:

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

Please help the gods to see, if you can doubt, thank you very much

Reply content:

Scenario Recurrence Process:

机器A局域网地址:192.168.9.8机器B局域网地址:192.168.9.9
分别配置机器A、B的hosts地址:配置机器A的地址:192.168.9.8 --> a.site.com 配置机器B的地址:192.168.9.9 --> b.site.com

Machine A is the front-end JS code, starts the service, and requests machine B on the browser (on behalf of the server side)
The first address of the http://b.site.com:8080/login/..., the request of the server side injected a cookie,
The key for setting the cookie is ' site ', value is ' date ', Doman is. site.com, path is/
, then open a new page on the browser and request Http://a.site.com:8080/index on the new page, which you can see through the chrome console
http://b.site.com:8080/
injected cookies, but ..... Through the Ajax interface, the cookie does not appear in the request header, tried a variety of ways, get, post, asynchronous, synchronization is not. Finally, using the Jsonp method, the cookie appears

The bad code is as follows:

$("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");        });});

The useful code is as follows:

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

Please help the gods to see, if you can doubt, thank you very much

Withcredentials=true, and ensure that the service side allows cross-domain, specifically refer to Cors

Domain is not what you say or what it is. Cookie domain and Ajax domain are not the same thing

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.