1

I want to get a JSON response from Flask backend to Angular app but could not find where the error is. The returning JSON response is:

{
  "phones": [
    {
    "age": 0, 
    "name": "Nexus S", 
    "snippet": "Fast just got faster with Nexus S."
    }, 
    {
    "age": 1, 
    "name": "Motorola XOOM with Wi-Fi", 
    "snippet": "The Next, Next Generation tablet."
    }, 
    {
    "age": 2, 
    "name": "MOTOROLA XOOM", 
    "snippet": "The Next, Next Generation tablet."
    }
  ]
}

And here is the controller:

function Phones($scope, $http){
$http.get('127.0.0.1:5000/phones').success(function(data){
$scope.phns = data.phones;
    console.log("I'm called");
});
}

This works when I pass the JSON object to the controller like this:

$scope.phones = [
{"name": "Nexus S",
 "snippet": "Fast just got faster with Nexus S.",
 "age": 0},
{"name": "Motorola XOOM with Wi-Fi",
 "snippet": "The Next, Next Generation tablet.",
 "age": 1},
{"name": "MOTOROLA XOOM",
 "snippet": "The Next, Next Generation tablet.",
 "age": 2}
];

It also doesn't work when I try to get data like data.phones. I'm sure the controller gets called but the get function does not get the data somehow.

5
  • First case is an object with an array in it... second one that works shows it defined as an array... why the difference? I guess what I'm saying is in the success you would have to assign data.phones to the $scope.phones. Also just console.log(data) to see what you're getting. Commented Jul 15, 2013 at 18:00
  • did u get response from server Commented Jul 15, 2013 at 18:01
  • I tried data.phones also but it did not work. Visiting 127.0.0.1:5000/phones on my browser returns the json at the top. I also used flask jsonify so I think the headers should be ok. When debugging looks like success callback is not called but I could not find why. Commented Jul 15, 2013 at 18:05
  • I'm sure that the success function is not getting called. I don't know why it doesn't. Commented Jul 15, 2013 at 18:11
  • It might be easier to debug if you post the entire Angular and Flask applications you are using. I assume they are both pretty short, or you can simplify them enough to be short? Commented Jul 16, 2013 at 0:07

1 Answer 1

2

Adding "http" to 127.0.0.1:5000/phones should solve the problem.

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

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.