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