PHP-AJAX CORS Fails due to Access-Control-Allow-Origin

大城市里の小女人 提交于 2019-12-06 15:05:08

You have allowed CORS Origin, so for access cross domain 3 headers (Origin, Methods, Headers) compulsory, see below sample headers

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");

@update: you can try this solution

header('Access-Control-Allow-Origin: *');
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            $headers=getallheaders();
            @$ACRH=$headers["Access-Control-Request-Headers"];
            header("Access-Control-Allow-Headers: $ACRH");
        }

header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");

try the following url:

$.ajax({
    type: "POST",
    url: 'http://127.0.0.1/MySpace',
    success: function(result) {
         console.log(result);
    },
    error: function() {
         console.log("error");
    },
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!