2013-07-27 2 views
0

이 코드를 사용하여 Yahoo Finance를 통해 CSV 파일을 다운로드하려고합니다.파일이 다운로드되지 않습니다.

$(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(); 
      }); 
     }); 
    }); 
}); 

경고를 표시 할 수 있습니다 (디버깅 용도로). 알림이 표시되지 않습니다. 이 코드에 문제가 있습니까? (일부 코드는 how to create an array by reading text file in javascript에서 가져옴)

쉬운 편집/도움말을 위해 jsFiddle입니다.

+2

동일한 출처 정책으로 인해 브라우저의 콘솔에 오류가 발생했습니다. 도메인 간 AJAX 요청을 그렇게 간단하게 만들 수는 없습니다. – Ian

+0

@Ian은 JS로 파일을 다운로드 할 수있는 방법입니까, 아니면 서버 측 언어를 사용해야합니까? – javadog36

+0

php의 cURL을 사용해보십시오 – uofc

답변

1

동일한 출처 정책에 의해 차단되었습니다.

옵션 :

  • 데이터 소스를 사용할 수 JSONP 또는이 CORS와 데이터에 대한 액세스를 제공하는 다른 서비스를 찾을 수 있습니다.
  • 사용하는 서버 측 프록시는 PHP로 데이터
+0

그래, 지금은 PHP 스크립트를 고수 할 것입니다. 고마워요 :) – javadog36

0

확인이 아웃 읽고, 당신은 당신의 요구에 맞게 할 수 있도록 조정할 수 있습니다.

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 
+0

PHP를 사용하려면 file_get_contents를 사용하는 것이 훨씬 쉽습니다. 각 자신의 – javadog36

+0

:) – uofc

관련 문제