


javascript - Why can cookies under the second-level domain name be obtained, but cannot be sent back automatically using ajax, and cookies cannot be cross-domain?
Scenario recurrence process:
<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>
The code that is not easy to use is as follows:Machine A is the front-end js code, starts the service, and requests the first address of machine B (representing the server side) on the browser http://b.site.com:8080/login/... , The server injects cookies at the same time as the request. Set the key of the cookie to 'site', the value to 'date', the doman to .site.com, and the path to /
. Then open a new page on the browser and make a request on the new page. http://a.site.com:8080/index, you can see the injected cookie through chrome console
http://b.site.com:8080/
, but... through ajax During the interface, the cookie did not appear in the request header. I tried various methods, including get, post, asynchronous, and synchronous, but none of them worked. Finally, the jsonp method was used, and the cookie appeared
<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>
The useful code is as follows:
<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>
Please ask the experts for help. If you can solve the problem, thank you very much
Reply content:
Scenario recurrence process:
<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>
. Then open a new page on the browser and make a request on the new page. http://a.site.com:8080/index, you can see the injected cookie through chrome consoleThe useful code is as follows:http://b.site.com:8080/
, but... through ajax During the interface, the cookie did not appear in the request header. I tried various methods, including get, post, asynchronous, and synchronous, but none of them worked. Finally, the jsonp method was used, and the cookie appeared
The code that is not easy to use is as follows:<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>Copy after loginCopy after login
<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>
Please ask the experts for help. If you can solve the problem, thank you very much
withCredentials=true, and ensure that the server allows cross-domain, please refer to cors
for details
A domain is not what you say it is. Cookie domain and ajax domain are not the same thing

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Detailed explanation of JavaScript code line-breaking skills When writing JavaScript code, we often encounter a line of code that is too long, which not only affects the readability of the code...
