1

i'm trying to download a csv file from a online realtime data source using javascript but the link of the source is not a direct link CSV SOURCE so i hope some one quite give a good exmaple or source that i could follow and also some example on using a loop to decoded the csv using javascript

Thanks in advance

Update Thanks guys i have managed to do some research from the link that you all gave me but there is one problem i cant seem to solve the No 'Access-Control-Allow-Origin' header is present on the requested resource. so could i request some help in this

 $("#postdata").click(function()
{
  csvData = $.ajax({
     headers: { 'Access-Control-Allow-Origin': '*'},
     Accept : "text/csv; charset=utf-8",
     url:"https://www.emcsg.com/marketdata/priceinformation",
     data:"downloadRealtime=true&5Periods=true",
     type:"GET",
     crossDomain: true,
     dataType:"text/csv",     
    success: function (result) {
                   alert(result);
                   alert("done!"+ csvData.getAllResponseHeaders())
    },
     error:function(jqXHR,textStatus,errorThrown)
     {
        alert("You can not send Cross Domain AJAX requests: "+errorThrown);
     }
    });

Thanks once again

Updated Problem solve turn out the website does not allow cors i switch to php here the code

<?php
$file= file_get_contents("https://www.emcsg.com/marketdata/priceinformation?downloadRealtime=true&5Periods=true");
$array = array_map("str_getcsv", explode("\n", $file));
print json_encode($array);
?>
Danvin Lee Qicheng
  • 57
  • 1
  • 2
  • 12
  • 3
    To do this only with javascript, you'll have to use ajax (with cross domain enabled, both on client and server side) to request the data. Then, parse the data as CSV. Post some piece of code, so we can help you. This link http://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript have some code, using jQuery. It parses a json data. You can use some jQuery plugin to parse CSV into JSON. – Christian Benseler Sep 02 '14 at 15:33
  • http://api.jquery.com/jquery.get/ – Donal Sep 02 '14 at 15:34
  • http://stackoverflow.com/questions/7431268/how-read-data-from-csv-file-using-javascript – Donal Sep 02 '14 at 15:36
  • @Donal Thanks for your respond but i am current stuck with Access-control issue . if you could take a look at my above code appreciated – Danvin Lee Qicheng Sep 03 '14 at 03:32
  • If you don't control the domain from which you are loading the data, and that domain doesn't add the appropriate `Access-Control-Allow-Origin` header, then you are stuck. There is nothing you can do – the browser will not let you download the file. You can either find another source for the same data, that is accessible across domains or build a server that you control that will server the data. – martineno Sep 03 '14 at 03:37
  • "No 'Access-Control-Allow-Origin' header is present on the requested resource" means that your javascript was downloaded from a different url (domain) than the url you are downloading the csv file from. The origin of your javascript is different than the origin of the csv file. – Donal Sep 03 '14 at 08:03
  • Have a look here: http://stackoverflow.com/questions/19821753/jquery-xml-error-no-access-control-allow-origin-header-is-present-on-the-req – Donal Sep 03 '14 at 08:07

0 Answers0