简体   繁体   中英

CORS header ‘Access-Control-Allow-Origin’ does not match ‘(null)’ on Ajax request

But the same error into browser

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://abc-test123.com/login. (Reason: CORS header 'Access-Control-Allow-Origin' does not match '(null)').

My Browser supports Cors

I have an extenstion of Cors for Chrome and CorsE ad-ons for Firefox

All API's working perfectly from same domain into my localhost server but one of our project API's are not working anymore

We are working on Slim Framework here is PHP file Code

$corsOptions = array(
    "origin" => "*",
    "exposeHeaders" => array("X-My-Custom-Header", "X-Another-Custom-Header"),
    "maxAge" => 1728000,
    "allowCredentials" => True,
    "allowMethods" => array(" PUT, GET, POST, DELETE, OPTIONS"),
    "allowHeaders" => array("X-PINGOTHER")
);
$cors = new \CorsSlim\CorsSlim($corsOptions);          
$app->add($cors);

I am calling ajax function(which was working before)

    var dataString = {
        username: "test",
        password: "test123"
    }
    $.ajax({
        method: "POST",
        url: varURI, //My url variable
        data: dataString,
        crossDomain: true,
        success: function(data)
        {
            if(data.status == "success")
            {
                alert("Success");
            }
            else
            {
                alert("Failure");
            }
        }
    });

I have tried below new things after API error

//(1)
$.support.cors = true;

//(2)
$.ajax({
    method: "POST",
    url: varURI, //My url variable
    data: dataString,
    dataType: "jsonp",
    async: false, //and true too
    crossDomain: true,
    success: function(data)
    {}
});

But the same error...

I have checked many questions and applied everything then after asked question here. So need help or any require info which I am missing here.

Add following code in your .htaccess

<ifModule mod_headers.c>
    Header set Access-Control-Allow-Origin: *
</ifModule>

And also look on answer from this link http://enable-cors.org/server_apache.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM