2

I am new to AngularJS and trying to fetch JSON data from a text file:

Here is my HTML:

<div ng-controller="customersController as custCont"> 
  <ul>
    <li ng-repeat="x in names">
      {{ x.Name + ', ' + x.Country }}
    </li>
  </ul>
</div>

Whereas my controller is as given below:

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';
    });        

This doesn't show anything. Now if I replace the above http request with test.txt data assigned to $scope.names then it starts working: I mean, something like this:

$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"
}
];

The text file obviously contains all the data except the first row (i.e. $scope.names = [ and the last semicolon;

That means the $http request to test.txt is failing which is in the same folder as HTML and JS files.

7
  • what is being displayed when you console.log(typeof response) in your callback? is it a string or an object? Commented Feb 7, 2015 at 21:05
  • Is it compulsory it will be a text file not a json file ? Commented Feb 7, 2015 at 21:11
  • The file format is json. Only its extension is .txt, plus I would also need to pull some text files as well. Commented Feb 7, 2015 at 21:12
  • @nanndoj I don't think its getting into any of those success() or error () functions Commented Feb 7, 2015 at 21:13
  • Just to be clear, your json in that text file is an array? Commented Feb 7, 2015 at 21:16

2 Answers 2

5

You are missing to define $http as a parameter

app.controller( "customersController", function( $scope, $window, $http) {

Also make sure you are testing in a web server. You cann't make ajax request from file:// protocol

Also change your request from POST to GET and it should work fine. Here is a Punklr

 method: 'GET',
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Yes I already noticed and corrected that. Calling from Server also gives the same error.
I am not using file:// protocol. But I am able to GET it from my local machine by simply using test.txt as URL.
Is it possible to get non JSON data also thru $http?
Yeah. But you have to change the "Content-Type": "application/json" to the one you want
0

(Posted answer on behalf of the question author).

There were two issues.

  1. I missed $http parameter in my controller function.
  2. I was using "POST", which I replaced with "GET" to make it work

It now work from local machine as well as remote web server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.