Cors 请求在 Excel 中工作但不能在 Javascript 中工作

2024-01-31

我了解 CORS 政策,并且由于服务器未设置为接受 CORS 请求,因此我应该无法从其他来源下载 CSV 文件。然而,当请求是通过 Excel 中的 VBA 发出时,服务器似乎非常乐意满足该请求。那么为什么我不能使用 Javascript 获取相同的文件呢?

在 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

在 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)
    })

所以上面的 Excel 代码对我来说没有问题,但是上面的 Javascript 代码给了我一个安全错误:

跨源请求被阻止:同源策略不允许读取远程资源https://subdomain.domain.com/specific_page/pending_csv?var=specificLocation https://subdomain.domain.com/specific_page/pending_csv?var=specificLocation。 (原因:CORS 标头“Access-Control-Allow-Origin”丢失)。

另外,我无法控制服务器。该应用程序由我公司的另一个团队控制(在不同的位置),我很惊讶 CORS 有效,因为我的域名是:mydomain.somebs.domain.com 他们的域名是:theirdomain.domain.com 所以我不能只需在他们的服务器策略中接受我的子域即可。

那么我应该有一种方法可以使用 Javascript 获取此资源,对吗?


这是因为 CORS 通常是一种浏览器策略,Excel 并不关心 CORS 策略,因为 Excel 并不容易受到浏览器每天面临的情况的影响(运行 javascript 的虚假网站从真实网站提取数据)。如果您正在做的只是一次性场景,您可以在浏览器中禁用 CORS 策略,但这不适用于生产网站。

我的建议是在服务器端脚本中使用一些 php 来下载 CSV,然后打印其内容。

就像是...

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

然后将您的 javascript url 指向您现在的“同一域”php 脚本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cors 请求在 Excel 中工作但不能在 Javascript 中工作 的相关文章

随机推荐