zoukankan      html  css  js  c++  java
  • node 跨域

    app.post('/api/list',function(req, res){
    let reqOrigin = req.headers.origin; // request响应头的origin属性
    res.header("Access-Control-Allow-Origin", reqOrigin);
    res.header('Access-Control-Allow-Credentials', 'true');
    userDao.usergetlist(req, res);
    });

    原文:http://www.cnblogs.com/hanxuming/p/8561999.html

    // 判断origin是否在域名白名单列表中
    function isOriginAllowed(origin, allowedOrigin) {
    if (_.isArray(allowedOrigin)) {
    for(let i = 0; i < allowedOrigin.length; i++) {
    if(isOriginAllowed(origin, allowedOrigin[i])) {
    return true;
    }
    }
    return false;
    } else if (_.isString(allowedOrigin)) {
    return origin === allowedOrigin;
    } else if (allowedOrigin instanceof RegExp) {
    return allowedOrigin.test(origin);
    } else {
    return !!allowedOrigin;
    }
    }


    const ALLOW_ORIGIN = [ // 域名白名单
    '*.233.666.com',
    'hello.world.com',
    'hello..*.com'
    ];

    app.post('a/b', function (req, res, next) {
    let reqOrigin = req.headers.origin; // request响应头的origin属性

    // 判断请求是否在域名白名单内
    if(isOriginAllowed(reqOrigin, ALLOW_ORIGIN)) {
    // 设置CORS为请求的Origin值
    res.header("Access-Control-Allow-Origin", reqOrigin);
    res.header('Access-Control-Allow-Credentials', 'true');

    // 你的业务代码逻辑代码 ...
    // ...
    } else {
    res.send({ code: -2, msg: '非法请求' });
    }
    });

  • 相关阅读:
    luogu P3238 [HNOI2014]道路堵塞
    luogu P3235 [HNOI2014]江南乐
    luogu P3237 [HNOI2014]米特运输
    luogu P3233 [HNOI2014]世界树
    luogu P3234 [HNOI2014]抄卡组
    luogu P3250 [HNOI2016]网络
    luogu P3201 [HNOI2009]梦幻布丁
    luogu P4148 简单题
    luogu P3767 膜法
    luogu P4314 CPU监控
  • 原文地址:https://www.cnblogs.com/weiyiyong/p/9117546.html
Copyright © 2011-2022 走看看