2017-01-14 1 views

답변

2

jQuery.getJSON() 도움이됩니다. 참조

링크 : JQuery.getJSON

$.getJSON("test.json", function(json) { 
    console.log(json); // this will show the info it in firebug console 
}); 


function loadJSON(callback) { 
    var xobj = new XMLHttpRequest(); 
     xobj.overrideMimeType("application/json"); 
    xobj.open('GET', 'my_data.json', true); // Replace 'my_data' with the path to your file 
    xobj.onreadystatechange = function() { 
     if (xobj.readyState == 4 && xobj.status == "200") { 
      // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode 
      callback(xobj.responseText); 
     } 
    }; 
    xobj.send(null); 
} 

function init() { 
loadJSON(function(response) { 
    // Parse JSON string into object 
    var actual_JSON = JSON.parse(response); 
}); 
} 
더 정보]에 대한
+0

감사합니다. 그것은 작동합니다. 각도로 사용하는 방법이 있습니까 –

+0

@Janukasamaranyake : 각도로 시도하지 않았습니다. 확인하셔야합니다. 도움이된다면 대답을 수락하십시오. –

+0

@Janukasamaranyake : Angular에 대한 링크가 있습니다. 해봐. 내 편이 맞아. [AngularJS_JSON] (http://stackoverflow.com/questions/21589340/read-local-file-in-angularjs) –

0

없음.

파일 시스템에서 직접이 아니라 HTTP를 통해 데이터를 읽는 경우 $http을 통해 Angular 초록이 XMLHttpRequest 객체 (또는 최신 브라우저에서 가져 오기)를 사용할 수 있습니다.

Firefox를 사용하는 경우 해당 방법을 사용하여 HTML 문서와 동일한 디렉토리 (또는 하위 디렉토리)에있는 로컬 파일을 읽을 수 있습니다. 다른 브라우저에는보다 엄격한 보안 규칙이 있습니다.

사용자가 파일을 선택하도록 허용 한 경우 (<input type="file">을 통해) FileReader API를 사용할 수 있습니다.

0
$http.get("timeZones.json") 
    .then(function onSuccess(response) { 
    // Handle success 
    var data = response.data; 
    var status = response.status; 
    var statusText = response.statusText; 
    var headers = response.headers; 
    var config = response.config;   
    //... 
    vm.timeZones = data; 
    }) 
    .catch(function onError(response) { 
    // Handle error 
    var data = response.data; 
    var status = response.status; 
    var statusText = response.statusText; 
    var headers = response.headers; 
    var config = response.config; 
    //... 
    }); 

AngularJS $http Service API Reference를 참조하십시오.

관련 문제