php cors error

/**
 *  An example CORS-compliant method.  It will allow any GET, POST, or OPTIONS requests from any
 *  origin.
 *
 *  In a production environment, you probably want to be more restrictive, but this gives you
 *  the general idea of what is involved.  For the nitty-gritty low-down, read:
 *
 *  - https://developer.mozilla.org/en/HTTP_access_control
 *  - https://fetch.spec.whatwg.org/#http-cors-protocol
 *
 */
function cors() {
    
    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
        // you want to allow, and if so:
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            // may also be using PUT, PATCH, HEAD etc
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
        exit(0);
    }
    
    echo "You have CORS!";
}

4.43
7
Phoenix Logan 186120 points

                                    Access-Control-Allow-Headers does not allow * as accepted value, see the Mozilla Documentation here.

Instead of the asterisk, you should send the accepted headers (first X-Requested-With as the error says).

4.43 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
cors in constructor php cors php urls allow cors in php heaers add cors in php alors cors php how to enable CORS on php file cors checking php fix cors in php php http cors solution for cors error php cors php headerss php cors options request what is CORS php php haders cors CORS headers PHP Enabling CORS support php error cors php in local php localhost cors cors php header how to allow cors in php php set cors header cors php headers cors access-control php tus-php cors make php cors where to put CORS in PHP how to activate CORS in PHP php add cors header how to solve cors error in php erro CORS php php returning cors how to allow cors php enable cors header php PHP and CORS php CORS policy X-cors php cors origin php phpdisable cors row php allow cors how to enable cors php adding CORS support php https php cors policy cors in php php cors issue enable the CORS php how to bypass cors php How to handle Php Cors? php cors func cors php localhost php cors header error php set header cors php force cors php add header cors php 5.6 cors error php cors error setup cors policy php cors error in php cors in localhost php php local server cors cors issue + PHP CORS error php how to enable cors in php api php get cors error what is cors in php enable cors php apache cors php cors php fix how to enable cors in php i php cors headers all all cors in php local php allow cors cors php enable how to enable cors in php php cors example php cors problem set CORS in php api php cors enable cors in php CORS Server php cors from a php server when i use cors in php cors php allow cors block php cors in php api all cors enable in php php set cors headers php enable cors cors problem in php php cors origin accept url php cros cors header php allow cors in php cors with php cors.php php cors install allow cors php php header access strict cors backend in php php cors header php cli server allow cors what is cors probleme accept cors header php access-control-allow-headers php php allow header strict-origin-when-cross-origin php php Access-Control-Allow-Methods php header for cors how to get around cors error with php php access control allow content type php cors allow php disable cors php cors whitelist allow method php php set header no cross php cros origin php server allow cors php allow cors CORS Php PHP cors set cors allow origin url pgp alloworigin cors https in php php activate http_origin
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source