내가 AngularJS와 새로운 오전과 텍스트 파일에서 JSON 데이터를 가져 오기 위해 노력 : 여기얻기 JSON 데이터는
내 HTML입니다 :
app.controller("customersController", function($scope, $window) {
$http({
url: 'test.txt',
dataType: 'json',
method: 'POST',
data: '',
headers: {
"Content-Type": "application/json"
}
}).success(function(response){
$scope.names = response;
}).error(function(error){
$scope.names = 'error';
});
아무 것도 표시되지 않습니다. 나는 $에 할당 TEST.TXT 데이터와 위의 HTTP 요청을 교체하면 이제 scope.names는이 작업을 시작합니다
$scope.names = [
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria"
},
{
"Name" : "FISSA Fabrica Inter. Salchichas S.A.",
"City" : "Madrid",
"Country" : "Spain"
},
{
"Name" : "Galería del gastrónomo",
"City" : "Barcelona",
"Country" : "Spain"
},
{
"Name" : "Island Trading",
"City" : "Cowes",
"Country" : "UK"
},
{
"Name" : "Königlich Essen",
"City" : "Brandenburg",
"Country" : "Germany"
},
{
"Name" : "Laughing Bacchus Wine Cellars",
"City" : "Vancouver",
"Country" : "Canada"
},
{
"Name" : "Magazzini Alimentari Riuniti",
"City" : "Bergamo",
"Country" : "Italy"
},
{
"Name" : "North/South",
"City" : "London",
"Country" : "UK"
},
{
"Name" : "Paris spécialités",
"City" : "Paris",
"Country" : "France"
},
{
"Name" : "Rattlesnake Canyon Grocery",
"City" : "Albuquerque",
"Country" : "USA"
},
{
"Name" : "Simons bistro",
"City" : "København",
"Country" : "Denmark"
},
{
"Name" : "The Big Cheese",
"City" : "Portland",
"Country" : "USA"
},
{
"Name" : "Vaffeljernet",
"City" : "Århus",
"Country" : "Denmark"
},
{
"Name" : "Wolski Zajazd",
"City" : "Warszawa",
"Country" : "Poland"
}
];
텍스트 파일은 분명히 첫 번째 행을 제외한 모든 데이터를 포함 : 나는이 같은 의미 (즉, $scope.names = [
및 HTML과 JS 파일과 같은 폴더에있는 실패 TEST.TXT 할 수있는 $의 HTTP 요청을 의미 마지막 세미콜론 ;
. 누군가는 도움을 기쁘게 할 수 있습니다.
감사합니다.
======= 업데이트 ========
두 가지 문제점이있었습니다.
- 컨트롤러 기능에 $ http 매개 변수가 누락되었습니다.
- 내가 대체 "POST"를 사용하고 있었다 "GET"는
그것은 이제 로컬 시스템뿐만 아니라 원격 웹 서버에서 작업 작동 확인합니다.
도움 주셔서 감사합니다.
콜백에서 console.log (응답 유형)을 표시 할 때 표시되는 내용은 무엇입니까? 문자열이나 객체입니까? – nanndoj
그것은 json 파일이 아닌 텍스트 파일일까요? – squiroid
파일 형식은 json입니다. 확장자는 .txt 뿐이며 일부 텍스트 파일도 가져와야합니다. – Anjum