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();
      });
     
  • 相关阅读:
    ACCP7.0-S2-复习自测-15测试分析
    线程
    多线程下的单例模式
    combobox 属性、事件、方法
    java的多线程总结
    爬虫--登录网页
    shell--字符串是否为空
    python--正则表达式 字符串匹配
    mysql---表所在数据库
    python--日期操作
  • 原文地址:https://www.cnblogs.com/ydam/p/10983576.html
Copyright © 2011-2022 走看看