2

I want to get data from a JSON file by using $http.get with AngularJS. I use the "new way" to write AngularJS scripts (link).

PLUNKER

Nothing happens, Firebug tells me nothing.

I think the problem is in my activate() function code, I don’t really understood the promises.

function activate() {
    return $http.get('test.json').then(function(data) {
        vm.result = data;
        return vm.result;
    });
} 

Have you an idea about this ?

7
  • Is your web server configured to serve .json static files? In IIS this would need to be added to the known mime types section. Check that whatever web server you are using is configured <mimeMap fileExtension=".json" mimeType="application/json"/> Commented Oct 5, 2015 at 9:48
  • I’m not sure of using a web server, I have just a .html page, a .js page and a .json page. I don’t think there is a server in this. Commented Oct 5, 2015 at 10:34
  • 1
    so you are running out of a local folder using FireFox. I think the $http ajax apis under the hood may not work properly in this case since I dought there is ever any XMLHttpRequest involved. Commented Oct 5, 2015 at 12:45
  • Oh ok, so I have to make some XMLHttpRequest. And did you meant "doubt"? Commented Oct 5, 2015 at 13:17
  • Correct in order to use $http you need some kind of web server to serve your files. Excuse my English :) Commented Oct 5, 2015 at 13:54

2 Answers 2

2

I see a couple of problems.

First, in your plunker code you have:

controller.$inject = ["$http"];

function controller() {

You are missing the $http parameter in your controller function signature.

function controller($http) {

Once I fixed that, I found your index.html page was binding to {{c.value}}, but your controller never defines a value property. Maybe this should be {{c.result}}? If I make these changes I get a visible result.

Sign up to request clarification or add additional context in comments.

4 Comments

If you're looking for "why isn't this working?", then this should help you along. If you want more information on how to write code that follows John Papa's community-contributed style guide, then you may want to post on Code Review. There are definitely a few points you've missed in the style guide when writing this code, but I'll assume this was a quick strap together trying to figure out how this all works.
FWIW, you can use [CodeReview.SE] to auto-resolve the link.
No problem, it also works with other sites as well. [Programmers.SE], etc. :)
Thank, classic errors :S And good advice :) But, as you can see in my comment of scniro response, there is a problem with $http :/
2

You're not able to return at that point in your then callback. Simply return the $http call itself, of which will be a promise, and resolve it. Also, your console should be telling you something like $http is undefined since you did not inject this service properly. Observe the following...

function activate() {
    return $http.get('test.json')
}

[...]

activate().then(function(response) {
    vm.result = response.data;
});

Plunker - demo


Side note - you'll likely want to wrap activate() into an reusable service to keep this logic out of our controllers, so we'll instead inject the defined service into controller instead of $http directly.

Plunker - demo with this logic wrapped in a simple service

6 Comments

vm.result = response.data;
Thanks, for your extra complete response :) But I think $http is not correctly implemented because Firebug tells me "TypeError: href is null" which, I saw, is a Firebug’s bug appearing when $http has an error. But why does it work on the Plunker and not on my computer?
that is odd. What else is different between your working copy and the plunker example?
@Carapatte have you made any progress on this? Is my answer able to help you?
Yes, thank, but I think it doesn’t work because I need some server to run $http (like cleftheris said).
|

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.