zoukankan      html  css  js  c++  java
  • node报错Request header field Content-Type is not allowed by

    当我在前端进行一个post的跨域请求时,控制台报错Request header field Content-Type is not allowed by Access-Control-Allow-Header。这是因为我在后端设置跨域请求的时候没有所需的请求类型,如图:
     
    app.all('*', function (req, res, next) {
      //响应头指定了该响应的资源是否被允许与给定的origin共享。*表示所有域都可以访问,同时可以将*改为指定的url,表示只有指定的url可以访问到资源 
        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();
      });
     
    ,所以直接在代码片段设置一下:
     
    app.all('*', function (req, res, next) {
      //响应头指定了该响应的资源是否被允许与给定的origin共享。*表示所有域都可以访问,同时可以将*改为指定的url,表示只有指定的url可以访问到资源 
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers",  " Origin, X-Requested-With, Content-Type, Accept");
        //允许请求资源的方式
        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();
      });
     
  • 相关阅读:
    Redis学习笔记--Redis数据过期策略详解
    网络带宽和速度测试windows和linux用iperf工具
    如何将rabbitmq集群中的某个节点移除.
    关于linux系统密码策略的设置
    linux 系统ssh超时设置
    linux安全 设置登录失败次数后,拒绝登录
    tomcat隐藏版本号
    第 16 章 模板与泛型编程
    第 15 章 面向对象程序设计
    第 14 章 重载运算与类型转换
  • 原文地址:https://www.cnblogs.com/ydam/p/10983576.html
Copyright © 2011-2022 走看看