Home > Web Front-end > JS Tutorial > Ajax cross-domain request COOKIE cannot be brought with the perfect solution

Ajax cross-domain request COOKIE cannot be brought with the perfect solution

韦小宝
Release: 2018-05-11 14:39:17
Original
4840 people have browsed it

This article mainly introduces the solution to the problem that COOKIE cannot be brought with Ajax cross-domain request. Friends who are interested in ajax can refer to the perfect solution to the problem that COOKIE cannot be brought with Ajax cross-domain request

1. Native ajax request method:

1 var xhr = new XMLHttpRequest();

2 xhr.open("POST", "http: //xxxx.com/demo/b/index.php", true);

3 xhr.withCredentials = true; //Supports sending cookies across domains

4 xhr.send() ;

2. jquery’s ajax post method request:

$.ajax({
    type: "POST",
    url: "http://xxx.com/api/test",
    dataType: 'json',
    // 允许携带证书
    xhrFields: {
       withCredentials: true
    },
    // 允许跨域
    crossDomain: true,
    success:function(){
    },
    error:function(){
  }
})
Copy after login

3. Server-side settings:

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://www.xxx.com");
Copy after login

The above is the perfect solution that the editor introduces to you when Ajax cross-domain request COOKIE cannot be brought. I hope it will be helpful to everyone! !

Related recommendations:

Example detailed explanation js combined with json to implement ajax simple example

Example detailed explanation ajax implementation paging query function

Detailed example of how to process data after ajax is submitted to the java background

The above is the detailed content of Ajax cross-domain request COOKIE cannot be brought with the perfect solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template