Ben
Ben

Reputation: 10298

Load data from ajax request (json) on startup in AngularJs

I have this simple page that just needs to show contents that is loaded from an external url (ajax request, response in json format)

I should say I'm a AngularJS newbie.

I've googled a bunch and found different ways of doing this and couldn't manage to determine which is the correct/simple/up-to-date way to achieve this.

My 2 challenges -

  1. Making the AJAX request run on startup (I can load the page before that happens and just load the contents one the ajax request finishes. Maybe show a 'Loading..' indicator)
  2. Doing a ajax request correctly.

Here is my attempt. I know that the ajax request is never made because its not setup correctly.

Upvotes: 0

Views: 588

Answers (2)

Rahul garg
Rahul garg

Reputation: 9362

You are getting into .error function: http://jsbin.com/oDUsuVA/3/edit

For jsonp your response should be something like:

callback([
{
    "title":"License Title 1",
    "licenseUrl":"http://cnn.com",
    "licenseText": " test"
}]);

Edit: You can simply do .get() request too, but if you had to use jsonp request interface, you would have to correct response.

A Jsonp request always wraps the logic into a json callback wrapper function.

Upvotes: 1

Jayram
Jayram

Reputation: 19588

I just did $http.get instead of your $http.jsonp and it did work for me.

Upvotes: 0

Related Questions