2016-08-31 4 views
2

Mootools JSONP를 사용하여 CSV 파일을 반환하는 URL에서 읽습니다. 이제 CSV (Adj Close)의 열 중 하나에 빈 공간이 생겨 파일 읽기를 중단하고 오류를 콘솔에 던집니다. csv 데이터를 읽을 수있는 옵션은 무엇입니까?csv 데이터를 읽으려고 할 때 Uncaught SyntaxError

나는 오류를 얻고있다 :

table.csv:1 Uncaught SyntaxError: Unexpected identifier

하나 개 더 추가 정보 : 나는 csv 파일이 브라우저에 다운로드 받고 볼 수 있습니다. 브라우저에서 읽을 수 없습니까?

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); 
    }; 
} 
+0

당신에게 하나의 URL에 대한 주위에 따옴표가 필요합니다 – mplungjan

+0

내 나쁜 ... 질문을 편집했습니다. 오타를 가리켜 주셔서 감사합니다. –

+1

URL이 JSONP 문자열을 반환하지 않고 CSV 파일을 반환합니다. – Sergio

답변

3

향후 참조를 위해, 여기에 answer I gave on GitHub 추가 :

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).

대답은 후속하는 질문을, 옵션 2에 대해 어떠한 결과가 없었다 이유에 대해 :

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; 
    } 
} 
관련 문제