Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

需要在跨域请求中携带另外一个域名下的 Cookie 该如何操作?【热度: 254】 #653

Open
yanlele opened this issue Dec 26, 2023 · 0 comments
Labels
Shopee 公司标签 网络 web 网络相关
Milestone

Comments

@yanlele
Copy link
Member

yanlele commented Dec 26, 2023

关键词:跨域 cookie

在跨域请求中携带另外一个域名下的 Cookie,需要通过设置响应头部的Access-Control-Allow-Credentials字段为true,并且请求头部中添加withCredentials字段为true。

在服务端需要设置响应头部的Access-Control-Allow-Origin字段为指定的域名,表示允许指定域名的跨域请求携带Cookie。

下面是一个示例代码(Node.js):

const express = require('express');
const app = express();

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', 'http://example.com');
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  next();
});

app.get('/api/data', (req, res) => {
  // 处理请求
  res.send('Response Data');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

在客户端发起跨域请求时,需要设置请求头部的withCredentials字段为true,示例代码(JavaScript):

fetch('http://example.com/api/data', {
  credentials: 'include',
})
  .then(response => response.text())
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

以上代码中,Access-Control-Allow-Origin设置为'http://example.com',表示允许该域名的跨域请求携带Cookie。fetch请求的参数中,credentials设置为'include'表示请求中携带Cookie。

@yanlele yanlele added Shopee 公司标签 网络 web 网络相关 labels Dec 26, 2023
@yanlele yanlele added this to the milestone Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Shopee 公司标签 网络 web 网络相关
Projects
None yet
Development

No branches or pull requests

1 participant