已解决
【Node.js】暴露自定义响应头和预检请求的时机
来自网友在路上 156856提问 提问时间:2023-10-26 04:37:07阅读次数: 56
最佳答案 问答题库568位专家为你答疑解惑
1. 暴露自定义响应头
// server.js
app.post('/api/user/hello', (req, res) => {res.setHeader('Access-Control-Allow-Origin', '*')// 权限设置(如果有个多,用 ,隔开),暴露给前端res.setHeader('Access-Control-expose-Headers', 'myHeader')// 后端自定义响应头res.set('myHeader', 123)res.json({ hello: 'world' })
})
// index.html
fetch('http://localhost:3000/api/user/hello', {method: 'POST',headers: {'Content-Type': 'application/json'}
}).then(res => {// 前端获取自定义响应头(前提:后端需要加一个权限)console.log(res.headers.get('myHeader'))return res.json()
}).then(response => {})
预检请求(options)的时机
- POST 请求并且
'Content-Type'
为'application/json'
。 - 跨域
- 自定义响应头
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"【Node.js】暴露自定义响应头和预检请求的时机":http://eshow365.cn/6-24781-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: linux 系统查询redis中指定key值(命令)
- 下一篇: 【疯狂Java】数组