Search code examples
javascriptcross-domainjsonpmootools

Uncaught SyntaxError when trying to reading csv data


I am using Mootools JSONP to read from an url which returns a CSV file. Now one of the columns in the CSV (Adj Close) has a blank space which aborts the reading of the file and throws out the error onto the console. What are my options, if any, to read the csv data?

I am getting the error:

table.csv:1 Uncaught SyntaxError: Unexpected identifier

One more additional information: I can see the csv file getting downloaded to the browser. Can I not read it from the browser?

var url = 'http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=31&f=2010&g=w&ignore=.csv';

  loaderJSONP(url);

loaderJSONP = function(URL) {
  //Get Data
  new Request.JSONP({
    url: URL,
    onSuccess: function(response) {
      show_response(response, $('post'));
    }
  }).send();

  show_response = function(obj, result) {
    console.log(obj);
  };
}

Solution

  • Adding the answer I gave on GitHub here, for future reference:

    The idea of JSONP is that the resource returns "JSON with Padding", where the JSON is a value in JavaScript Object Notation, and the Padding is a function call, with as aim to get around same-origin policies.

    • JSON: {"a":"b"}
    • JSONP: myCallbackFunction({"a":"b"})

    So basically, the JSONP resource is expected to return valid, executable JavaScript code, and you (or in this case MooTools More) provide(s) the definition of "myCallbackFunction". The resource location is then appended with &callback=myCallbackFunction and injected as src of an HTML <script> tag. The tag is injected into the document, and the function is called with the data supplied, so you can go about your things. (The function itself won't be called "myCallbackFunction" by MooTools, in case you're wondering.)

    The resource you request from ichart.yahoo.com returns a CSV format and I doubt that it supports any callback function padding, meaning it probably can't work this way.

    I can see two options:

    1. Look at Yahoo's developer console, see if the yahoo.finance.historicaldata table suits your needs, or if there is another way to fetch your data through YQL.
    2. Fetch the CSV through other means (some server), and supply it back to your application (with optional pre parsing, so you don't have to parse the CSV in JavaScript).

    Answer to follow-up question, regarding option 2, about why there were no results:

    You'll have to use the callback value passed as query parameter.

    Try something like this (untested):

    function print_json($data) {
        $json = json_encode($data);
    
        if (array_key_exists('callback', $_GET) && !preg_match('/[^\w.]/', $_GET['callback'])) {
            header('Content-type: application/javascript; charset=utf-8');
            echo $_GET['callback'], '(', $json, ')';
        } else {
            header('Content-type: application/json; charset=utf-8');
            echo $json;
        }
    }