首页 > 其他 > 详细

4、CORS跨域请求限制与解决(预请求)

时间:2018-12-15 20:13:11      阅读:195      评论:0      收藏:0      [点我收藏+]

test.html

<script>
    fetch(‘http://localhost:8887/‘, {
      method: ‘PUT‘,
      headers: {
        ‘X-Test-Cors‘: ‘123‘
      }
    })
</script>

 

server.js

const http = require(‘http‘)

http.createServer((request, response) => {
  console.log(‘request come‘, request.url)

  // 多个Access-Control-Allow-Origin只需通过request的host动态判断
  response.writeHead(200, {
    ‘Access-Control-Allow-Origin‘: ‘*‘, // 这里可以限制相关ip
    ‘Access-Control-Allow-Headers‘: ‘X-Test-Cors‘, // 允许的请求头
    ‘Access-Control-Allow-Methods‘: ‘POST, PUT, Delete‘, // 默认允许GET、HEAD、POST
    ‘Access-Control-Max-Age‘: ‘1000‘  // 1000s之内不需要发送预请求验证
  })
  response.end(‘123‘)

}).listen(8887)

console.log(‘server listening on 8887‘)

 

4、CORS跨域请求限制与解决(预请求)

原文:https://www.cnblogs.com/zouxinping/p/10124417.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!