localhost is therefore not allowed access

前端 未结 3 1014
梦如初夏
梦如初夏 2021-01-20 11:19

To solve CORS issue, I wrote there

header(\'Access-Control-Allow-Origin: *\');
header(\'Access-Control-Allow-Methods: GET, POST\');
header(\"Access-Control-A         


        
3条回答
  •  借酒劲吻你
    2021-01-20 11:28

    Using * will not work. The below PHP code will accept all requests from all domains and works in IE, Firefox, Chrome and Safari.

    $origin=isset($_SERVER['HTTP_ORIGIN'])?$_SERVER['HTTP_ORIGIN']:$_SERVER['HTTP_HOST'];
    header('Access-Control-Allow-Origin: '.$origin);        
    header('Access-Control-Allow-Methods: POST, OPTIONS, GET, PUT');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Headers: Authorization, X-Requested-With');
    header('P3P: CP="NON DSP LAW CUR ADM DEV TAI PSA PSD HIS OUR DEL IND UNI PUR COM NAV INT DEM CNT STA POL HEA PRE LOC IVD SAM IVA OTC"');
    header('Access-Control-Max-Age: 1');
    

    Accepting requests from all domains is insecure. For a better (but slightly more complex) solution, see here: CORS That Works In IE, Firefox, Chrome And Safari

提交回复
热议问题