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

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

韦小宝
韦小宝Original
2018-05-11 14:39:174784browse

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(){
  }
})

3. Server-side settings:

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

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!

Statement:
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