博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs 解决跨域
阅读量:7015 次
发布时间:2019-06-28

本文共 1877 字,大约阅读时间需要 6 分钟。

1.失败
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();});

  

2.失败
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}

  

3. app.all('*', function (req, res, next) {    res.header("Access-Control-Allow-Origin", "*");    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");    res.header("Access-Control-Allow-Credentials", false);    res.header("Access-Control-Max-Age", '86400');    res.header("Access-Control-Allow-Headers", "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept");    res.header("Content-Type", "application/json;charset=utf-8");    res.header("X-Powered-By", ' 3.2.1');    next();});

  

“简单请求”

 请求方法是以下三种方法之一:

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...

转载于:https://www.cnblogs.com/tongbiao/p/9893249.html

你可能感兴趣的文章
PID 29364
查看>>
设计模式-状态模式(25)
查看>>
group by查询每组时间最新的一条记录
查看>>
struts2--前台数据通过参数传给后台,后台如何获取参数
查看>>
HDU 4054 Hexadecimal View【模拟】【字符串处理】
查看>>
配置cordova的android开发环境(无android studio)
查看>>
监控glusterfs
查看>>
tomcat 启动慢问题
查看>>
map-reduce流程图
查看>>
【经验】CentOS 5.2 下用Yum安装Apache+PHP+MySQL环境
查看>>
linux centos service 参数详解
查看>>
利用层次遍历原理构建二叉树
查看>>
集体编程智慧(发现的一些代码问题)
查看>>
LeetCode Notes_#5 Longest Palindromic Substring
查看>>
swift 苹果开发者cocoachina学习网站 http://www.cocoachina.com/swift/
查看>>
Apache的配置详细解
查看>>
【C++ Primer】两个类相互包含的求解策略
查看>>
OpenCV学习(7.14)
查看>>
接口测试3A原则
查看>>
RMAN备份脚本--DataGuard primary
查看>>