Tyler Mitchell
Tyler Mitchell

Reputation: 57

Cors request working in Excel but not in Javascript

I understand the CORS policy and since the server is not set to accept CORS requests, I should not be able to download the CSV file from a different origin. However the server seems perfectly happy to fullfill the request when it is made from VBA in Excel. So why can't I grab the same file using Javascript?

In Excel:

Sub transfercsv()
sCSVLink = "https://subdomain.domain.com/specific_page/pending_csv?var=specificLocation"
sfile = "Filename_Specific_Location_" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & ".csv"
ssheet = "CSV Transfer"
Set wnd = ActiveWindow
Application.ScreenUpdating = False
Sheets(ssheet).Cells.ClearContents
Workbooks.Open Filename:=sCSVLink
Windows("pending_CSV").Activate
ActiveSheet.Cells.Copy
wnd.Activate
Sheet1.Select
Sheets("CSV Transfer").Range("A1").Select
Sheets("CSV Transfer").Paste
Application.DisplayAlerts = False
Windows("pending_CSV").Activate
Windows("pending_CSV").Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Call anotherFunc
End Sub

In Javascript:

function displayData(){
    //this function takes a csv and displays it as a table in the page
  var tabulate = function (data,columns) {
      var table = d3.select('body').append('table')
        var thead = table.append('thead')
        var tbody = table.append('tbody')
        thead.append('tr')
          .selectAll('th')
            .data(columns)
            .enter()
          .append('th')
            .text(function (d) { return d })
        var rows = tbody.selectAll('tr')
            .data(data)
            .enter()
          .append('tr')
        var cells = rows.selectAll('td')
            .data(function(row) {
                return columns.map(function (column) {
                    return { column: column, value: row[column] }
              })
          })
          .enter()
        .append('td')
          .text(function (d) { return d.value })
      return table;
    }
    //this function actually makes the xhr request and gets the csv
    d3.csv('https://subdomain.domain.com/specific_page/pending_csv?var=specificLocation',function (data) {
        var columns = ['column_name_1','column_name_2','column_name_3','column_name_4','column_name_5','column_name_6','column_name_7','column_name_8','column_name_9','column_name_10','column_name_11']
      tabulate(data,columns)
    })

So the Excel code above works for me no problem, but the Javascript code above gives me a security error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://subdomain.domain.com/specific_page/pending_csv?var=specificLocation. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Also, I have no control over the server. That app is controlled by a different team at my company(In a different location) I am surprised that CORS is in effect because my domain is: mydomain.somebs.domain.com Theirs is: theirdomain.domain.com So I can't just accept my subdomain in their server policy.

So there should be a way for me to get this resource with Javascript right?

Upvotes: 2

Views: 1512

Answers (1)

acolchagoff
acolchagoff

Reputation: 1978

Its because CORS is typically a browser policy, Excel doesn't care about CORS policy because Excel isn't really vulnerable to a scenario that browsers face every day (fake websites running javascript that pulls data from the real site.) If what you're doing is just a one off scenario you can disable CORS policy in your browser, but that won't work for a production website.

My advice would be to use some php in a server side script to download the CSV and then print its contents..

Something like...

<?php
echo file_get_contents('http://otherdomain.com/that_csv_file.csv');
?>

Then point your javascript url at your now 'same domain' php script.

Upvotes: 3

Related Questions