GorvGoyl
GorvGoyl

Reputation: 49220

download a file from url - Javascript ajax

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

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

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

$.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?

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.

Upvotes: 1

Views: 344

Answers (1)

guest271314
guest271314

Reputation: 1

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

You can use $.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>

Upvotes: 2

Related Questions