从url - Javascript ajax下载文件

时间:2022-01-03 09:52:34

There's a CSV file uploaded to the server that I want to parse using javascript/jquery.

有一个CSV文件上传到服务器,我想用javascript/jquery来解析。

I'm trying to get the file using ajax call but it's always giving me error.:

我尝试使用ajax调用来获取文件,但它总是给我错误。

XMLHttpRequest cannot load https://1fichier.com/?w5hfqz60tk&_=1474818392318. No 'Access-Control-Allow-Origin' header is present on the requested resource.

XMLHttpRequest不能加载https://1fichier.com/?w5hfqz60tk&_=1474818392318。在请求的资源上不存在“访问控制允许起源”的标头。

$.ajax({
        url:'https://1fichier.com/?w5hfqz60tk',
        type: "GET",
        dataType: "text",

        success: function (data){
          parseFile(data);

        },
        error:function(e){

        }
    });

I need to run the above code in jsFiddle.. How can I bypass this error?

我需要在jsFiddle中运行上面的代码。如何绕过这个错误?

Or is there any alternative way to download a file?

或者有没有其他的方式下载文件?

Update: I just found out that adding url like this: https://crossorigin.me/MY_HTTP(S)_LINK solved my problem but I'm looking for an authentic way.

更新:我刚刚发现添加这样的url: https://crossorigin.me/MY_HTTP(S)_LINK解决了我的问题,但我正在寻找一种真实的方式。

1 个解决方案

#1


2  

How can I bypass this error? Or is there any alternative way to download a file?

如何绕过这个错误?或者有没有其他的方式下载文件?

You can use $.getJSON(), YQL

您可以使用$.getJSON()、YQL

var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D'https%3A%2F%2F1fichier.com%2F%3Fw5hfqz60tk'%0A&format=json&callback="

$.getJSON(url, function(data) {
  console.log(data.query.results)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#1


2  

How can I bypass this error? Or is there any alternative way to download a file?

如何绕过这个错误?或者有没有其他的方式下载文件?

You can use $.getJSON(), YQL

您可以使用$.getJSON()、YQL

var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D'https%3A%2F%2F1fichier.com%2F%3Fw5hfqz60tk'%0A&format=json&callback="

$.getJSON(url, function(data) {
  console.log(data.query.results)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>