Ajax 跨域请求

image

Ajax跨域请求 Access-Control-Allow-Origin 大多是使用*通配符放行

放行单个域名

正则方式
php

    $http_origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';if (preg_match('/domain1.com|domain2.com$/i', $http_origin)) {
    header('Access-Control-Allow-Origin: ' . $http_origin); }

Nginx

    location / {
    if ($http_origin ~ 'domain1.com|domain2.com$') {
        add_header Access-Control-Allow-Origin $http_origin;
    }}

普通方式
php

    header('Access-Control-Allow-Origin: http://domain1.com');
Nginx

    add_header Access-Control-Allow-Origin http://domain1.com;

放行所有域名
php

    header('Access-Control-Allow-Origin: *');
Nginx

    add_header Access-Control-Allow-Origin *;
Last modification:August 30th, 2018 at 11:53 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment