怎么在uni-app响应头里设置多个cookie

怎么在uni-app响应头里设置多个cookie

exports.main = async (event, context) => {  
    //event为客户端上传的参数  
    console.log('event : ', event)  

    //返回数据给客户端  
    return {  
    mpserverlessComposedResponse: true, // 使用阿里云返回集成响应是需要此字段为true  
    isBase64Encoded: false,  
    statusCode: 200,  
    headers: {  
        'set-cookie': 'cookie1=value1',//这种方式只能设置一个cookie  
    },  
        body:'a'  
}  
};
开发环境 版本号 项目创建方式
未提及 未提及 未提及

更多关于怎么在uni-app响应头里设置多个cookie的实战教程也可以访问 https://www.itying.com/category-93-b0.html

8 回复

多个之间用;隔开不行吗?

更多关于怎么在uni-app响应头里设置多个cookie的实战教程也可以访问 https://www.itying.com/category-93-b0.html


不行

使用数组形式 exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)

//返回数据给客户端    
return {    
mpserverlessComposedResponse: true, // 使用阿里云返回集成响应是需要此字段为true    
isBase64Encoded: false,    
statusCode: 200,    
headers: {    
    'set-cookie': ['cookie1=value1; Max-Age=900; HttpOnly', 'cookie2=value2; Max-Age=900; HttpOnly']  
},    
    body:'a'    

}
};

这种方式我试了不行

哪个云厂商?

阿里云

回复 1***@qq.com: 阿里云目前不支持

在uni-app中设置多个cookie可以通过以下方式实现:

  1. 使用数组形式设置多个set-cookie头:
headers: {
    'set-cookie': ['cookie1=value1', 'cookie2=value2']
}
  1. 或者在阿里云函数计算中,可以这样写:
headers: {
    'set-cookie': 'cookie1=value1; Path=/',
    'set-cookie2': 'cookie2=value2; Path=/'
}
  1. 如果需要设置cookie属性(如过期时间、域名等),可以这样:
headers: {
    'set-cookie': [
        'cookie1=value1; Expires=Wed, 21 Oct 2025 07:28:00 GMT; Path=/',
        'cookie2=value2; Domain=example.com; Path=/; Secure'
    ]
}
回到顶部