请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

带cookie的跨域请求

老师 请问带cookie的跨域请求 是指前端携带cookie发出去的请求吗?可是我用node req.headers.cookie 读取不到前端的cookie(设置了相关的头 前端也加了withcred....) 如何查看携带的cookie 有啥应用场景?


node 端

var express = require('express')


var app = express()


app.all('/textcookie', function (req, res, next) {

res.header('Access-Control-Allow-Origin', 'http://localhost:3000')

res.header('Access-Control-Allow-Methods', 'PUT, GET, POST')

res.header('Access-Control-Allow-Headers', 'X-Requested-With,If-Modified-Since,Cache-Control,Content-Type')

res.header('Access-Control-Allow-Credentials', 'true')

res.header("P3P","CP=CAO PSA OUR");

res.cookie('name', 'tobi', { domain: 'localhost', path: '/', secure: true });


// console.log(req.headers.cookie)

res.send(req.headers.cookie)


})


app.listen(8081)





html端

$.ajax({

type: 'post',

url: 'http://localhost:8081/textcookie',

xhrFields: {

withCredendtials: true

},

crossDomain: true,

success: function (result) {

console.log(result)

}

})


正在回答 回答被采纳积分+3

1回答

newming 2018-03-17 08:50:51

https://github.com/expressjs/cookie-parser


express 的话可以使用这个中间件

1 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信