How to solve 'Redirect has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header'?

961,952

Solution 1

In addition to what awd mentioned about getting the person responsible for the server to reconfigure (an impractical solution for local development) I use a change-origin chrome plugin like this:

Moesif Orign & CORS Changer

You can make your local dev server (ex: localhost:8080) to appear to be coming from 172.16.1.157:8002 or any other domain.

Solution 2

Thanks all, I solved by this extension on chrome.

Allow CORS: Access-Control-Allow-Origin

Solution 3

If you have control over your server, you can use PHP:

<?PHP
header('Access-Control-Allow-Origin: *');
?>

Solution 4

Ask the person maintaining the server at http://172.16.1.157:8002/ to add your hostname to Access-Control-Allow-Origin hosts, the server should return a header similar to the following with the response-

Access-Control-Allow-Origin: yourhostname:port

Solution 5

When you have this problem with Chrome, you don't need an Extension.
Start Chrome from the Console:

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

Maybe you have to close all Tabs in Chrome and restart it.

Share:
961,952
Parkar
Author by

Parkar

Updated on July 08, 2022

Comments

  • Parkar
    Parkar almost 2 years

    I am working on an app using Vue js. According to my setting I need to pass to a variable to my URL when setting change.

    <!-- language: lang-js -->
    
        $.get('http://172.16.1.157:8002/firstcolumn/' + c1v + '/' + c1b, function (data) { 
          // some code...
        });
    

    But when my app hit on URL, it shows the following message.

    Failed to load http://172.16.1.157:8002/firstcolumn/2017-03-01/2017-10-26: Redirect from 'http://172.16.1.157:8002/firstcolumn/2017-03-01/2017-10-26' to 'http://172.16.1.157:8002/firstcolumn/2017-03-01/2017-10-26/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
    
  • user5014677
    user5014677 over 6 years
    @RoryMcCrossan it says origin is localhost, so cors get triggered. I think?
  • Black
    Black about 6 years
    Im not sure how to set it up, can you explain further? Screenshots would be nice.
  • tw1742
    tw1742 over 5 years
    @altShiftDev Does this plugin have any options to handle: "Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request."?
  • kabrice
    kabrice over 4 years
    The link is now broken :(
  • Cyber
    Cyber over 4 years
    broken link, no description at all.
  • Cyber
    Cyber over 4 years
    added into my .htaccess of local project and works like charm, don't forget to reboot your server / mamp / apache, whatever you use.
  • Cyber
    Cyber over 4 years
    better add to the .htaccess file, this would apply to the entire project and not just to the sites you have added this snippet.
  • ewalel
    ewalel over 4 years
    Thanks this helps to avoid all the hassle and test the code from localhost.
  • Peter VARGA
    Peter VARGA over 4 years
    Update the link my friend!
  • Jordan Noel
    Jordan Noel over 4 years
  • OM Bharatiya
    OM Bharatiya over 4 years
    Hey, the chrome extension link provided is broken. Can you please update the answer?
  • SJX
    SJX about 4 years
    You don't need an Extension for Chrome: stackoverflow.com/a/60950835/2280067
  • Nitin Nanda
    Nitin Nanda over 3 years
    the extension is just a temporary fix and not a solution to the problem
  • MeVR
    MeVR about 3 years
    may i know how to solve this from angular side?
  • andromeda
    andromeda about 3 years
    This is not a solution. Are you going to ask everyone to install a chrome extension?
  • Mike Q
    Mike Q about 3 years
    This didn't seem to work for me, it broke the API call actually.
  • Lepy
    Lepy about 3 years
    Try to put your real ip instead of the localhost. Try to google your ip and replace 'localhost' with that @Black
  • Jagadish Nallappa
    Jagadish Nallappa almost 3 years
    Thank you so much! This worked... Cheerio!
  • Darius.V
    Darius.V over 2 years
    for me it does not even reach php script
  • Kasyful Anwar
    Kasyful Anwar over 2 years
    This is an extension for Chrome
  • Arslan Ahmad khan
    Arslan Ahmad khan over 2 years
    great suggetion
  • Dogunbound hounds
    Dogunbound hounds over 2 years
    Doesn't work buddy.
  • Vixxd
    Vixxd over 2 years
    A word of warning: the Moesif Origin & CORS Changer plug-in requires you enter a work-related e-mail address to access the advanced settings. I would not recommend.
  • Pankaj Kumar
    Pankaj Kumar almost 2 years
    Thanks,Thanks .... It solved my headache.