Question

I'm attempting to download a CSV file through Yahoo Finance with this code.

$(function () {
    $(document).ready(function() {
        $.get("http://download.finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1ohg&s=AAPL", function(data) {
            var output = data.split(new RegExp(",|\r")).map(function (element) {
                alert($.trim(element).toLowerCase());
                return $.trim(element).toLowerCase();
            });
        });
    });
});

You can see I put the alert in there (for debugging purposes) but I'm not getting the alert. Is there something wrong with this code? (some of the code was taken from how to create an array by reading text file in javascript)

Here's a jsFiddle for easy edits/help.

Was it helpful?

Solution

This is blocked by same-origin policy.

Options:

  • find other service that provides access to the data with JSONP or have CORS enabled for the data source.
  • use server side proxy to read data

OTHER TIPS

Check this out with php, you can tailor make it to suit your needs.

function queryphp($url)
{
    $portal = curl_init(); 

    curl_setopt($portal, CURLOPT_URL, $url); 

    curl_setopt($portal, CURLOPT_RETURNTRANSFER, 1); 

    $output = curl_exec($portal); 
    if(!($output))
        header('Location: http://www.yourwebsite.com/errorpage.php');

    curl_close($portal); 

    return $output;
}//example usage:
    //$page_data = queryphp("http://www.whatever.com/whateverpage.php[?var1=whatever&var2=whatever"]);
    //now you have the output from whateverpage.php saved as a string; which you can append anywhere to your current page's output. #repetitive code reduction
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top