express的使用:接口的编写(三)

1.接口的跨域问题

  a.CORS,主流

  b.JSONP,只支持get请求

 步骤:a.安装  npm install cors

    b.使用  const cors = require('cors') 导入中间件

    c.在路由之前调用app.use(cors()) 配置中间件

2.CORS响应头部

  a.Access-Control-Allow-Origin:<origin>|*

  b.Access-Control-Allow-Headers (9个)

  c.Access-Control-Allow-Method 默认支持post get head

3.cors分类

  a.简单请求 1. post get head 2.请求头在指定的9个之内

  b.预检请求 1.请求类型为post get head之外的类型 2.包含自定义头部字段 3.向服务器发送了application/json 格式数据

posted @ 2022-12-22 17:09  月下云生  阅读(34)  评论(0)    收藏  举报