分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

nodejs 解决跨域

发布时间:2023-09-06 02:20责任编辑:郭大石关键词:jsnodejs跨域
app.all(‘*‘, function (req, res, next) { ???res.header("Access-Control-Allow-Origin", "*"); ???res.header("Access-Control-Allow-Headers", "X-Requested-With"); ???res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); ???res.header("X-Powered-By", ‘ 3.2.1‘); ???res.header("Content-Type", "application/json;charset=utf-8"); ???next();});

  

if (req.method === ‘OPTIONS‘) { ?????console.log(‘!OPTIONS‘); ?????var headers = {}; ?????// IE8 does not allow domains to be specified, just the * ?????// headers["Access-Control-Allow-Origin"] = req.headers.origin; ?????headers["Access-Control-Allow-Origin"] = "*"; ?????headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"; ?????headers["Access-Control-Allow-Credentials"] = false; ?????headers["Access-Control-Max-Age"] = ‘86400‘; // 24 hours ?????headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept"; ?????res.writeHead(200, headers); ?????res.end();} else {//...other requests}

  

“简单请求”
 请求方法是以下三种方法之一:
????HEAD
????GET
????POST

“非简单请求”
HTTP的头信息不超出以下几种字段:
????Accept
????Accept-Language
????Content-Language
????Last-Event-ID
????Content-Type:
????????application/x-www-form-urlencoded、 multipart/form-data、text/plain
不满足这些特征的请求称为“非简单请求”,例如:content-type=applicaiton/json , method = PUT/DELETE...

nodejs 解决跨域

原文地址:https://www.cnblogs.com/tongbiao/p/9893249.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved