0

I've been trying to workout if it's possible for me to get rid of my 'Access-Control-Allow-Origin' error.

I'm requesting data from some reports that come back as CSV files but for them to work i have to use a chrome plug in that disables CORS.

after reading tons of stack over flow questions and lots of googling i still can't tell whether it's something I'm not putting in my code or if it's a setting where the data is stored that needs to turn it on.

here is my code that requests the data, bare in mind this works just only with the plug in and my API key has been removed.

function Getfueltype(){

    j=JSON.stringify

    $.ajax({
        url: 'https://api.bmreports.com/BMRS/FUELINSTHHCUR/v1?APIKey=&ServiceType=CSV',
        async: false,
        success: function (csvd) {
            data = $.csv.toArrays(csvd);
        },
        dataType: "text",
        complete: function () {         
            while (n < (data.length)-2) {
                fueltype = j( data[n].slice(1,2));
                GigaWatt = j(parseFloat(data[n].slice(2,3)));
                arr.push(fueltype);
                arr2.push(GigaWatt);

                n++
            }

            drawChart2(data);

        }
    });
}
adam Wadsworth
  • 774
  • 1
  • 8
  • 26
  • You can't, that the point of CORS. It's a security mechanism to prevent people doing what you want to do. – Liam May 02 '18 at 13:01
  • 1
    You can use a CORS proxy—either an open public one, or else you can set up one in literally just 2-3 minutes on Heroku. For details, see the *How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems* section of the answer at https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141 – sideshowbarker May 02 '18 at 13:03

1 Answers1

0

If you are attempting this on a local machine to test the code before it goes live on a server, I used --allow-file-access-from-files flag for my chrome.

This allows you to bypass No Access-Control-Allow-Origin header problem.

Example:

  1. Create a copy of chrome.exe in the installation folder.
  2. Send that copy to the desktop as a shortcut.
  3. Right-click on Chrome shortcut and paste the flag at the end of the Target box. (See example string below)

Target - "C:\Users\SuperCoolUser\AppData\Local\Google\Chrome\Application\chrome-copy.exe" --allow-file-access-from-files

Hope that helped even the slightest bit.

Alex
  • 2,164
  • 1
  • 9
  • 27