CORS跨域限制以及预请求验证

cors包含的限制:

  默认允许方法:

    GET、HEAD、POST

  默认允许的Content-Type

    text/plain、multipart/form-data、application/x-www-form-urlencoded

    这三个也就是html的form表单可以设置的数据类型

  请求头的限制

    哪些请求头被限制哪些是默认允许的可以参考:

    https://fetch.spec.whatwg.org/#cors-safelisted-request-header

  XMLHttpRequestUpload对象均没有注册任何事件监听器

  请求中没有使用ReadableStream对象

预请求验证:

const http = require('http')

http.createServer((req, res) => {
  console.log('request come', req.url)

  res.writeHead(200, {
    'Access-Control-Allow-Origin': '*',   // 允许访问我这个服务的网址
    'Access-Control-Allow-Header': 'X-Test-Cors', // 允许访问我这个服务的头
    'Access-Control-Allow-Methods': 'POST, PUT, Delete', // 允许访问我这个服务的方法
    'Access-Control-Max-Age': '1000'  // 多长时间内不需要再次发送预请求
  })
  res.end('123')
}).listen(8887)

console.log('server listening on 8887')

 

posted @ 2020-02-20 10:29  ladybug7  阅读(616)  评论(0编辑  收藏  举报