Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am trying to call an api from AngularJS and render the response on the web browser.

Server

@app.route("/data")
def getDataFromDB():
      cur.execute("select * from employee")
      rows = cur.fetchall()
      columns = [desc[0] for desc in cur.description]
      result = []
      for row in rows:
          row = dict(zip(columns, row))
          json_row=json.dumps(row)
          result.append(json_row)
          json_response=json.dumps(result)
     response=Response(json_response,content_type='application/json; charset=utf-8')
     response.headers.add('content-length',len(json_response))
     response.status_code=200
     return response

Client

maincontroller.js

  var app=angular.module('myApp',[]);
  app.controller("MainController", function($scope,$http){

  var done=function(resp){

  $scope.lists=resp.data;
  };
  var fail=function(err){

  };

 $http.get('http://10.62.XX.XX:8083/data')
 .then(done,fail);

});

index.html

<!DOCTYPE html>
<head>
<title>Learning AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"     
type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<script src="maincontroller.js" type="text/javascript"></script>

</head>
<body ng-app="myApp">

<div id='content'  ng-controller='MainController'>

 <div>
   {{lists}}
 </div>

 </div>
 </body>
 </html>

Now, when I access the above code using jsbin.com, I can see my api getting called but nothing is visible on the output screen in jsbin. It is blank. But when I put the same code in eclipse, I see no api call happening. Do I need to do something more to make angularJS work? Currently, I just open the index.html with web browser and expecting it to hit the api and get the result.

It would be great if someone could provide me a working demo of the api call using my code snippet. I want to know what I am missing out.

share|improve this question
    
duplicate stackoverflow.com/questions/24792831/… –  Navaneethan Jul 17 '14 at 7:56

2 Answers 2

up vote 1 down vote accepted

TL;DR

You need to return index.html from @app.route("/") when you run your flask server. Update the script src to mach your flask config.

Server

  • It's better to use Flask-SQLAlchemy

  • instead of creating response use jsonify

    from flask import jsonify
    @app.route("/data")
    ....
        return jsonify(result), 200
    

Client

  • Checkout resource
  • What is http://10.62.XX.XX:8083/data? Use http://127.0.0.1:5000/data
  • Don't shallow error. Add at least console.log(err) to debug

Running

How do you open index.html? As a file? You need an http server will server for html and js from the same domain that you make the get request in maincontroller.js. Use Flask for that. Flask assumes certain structure of the project. Here is a seed https://github.com/rxl/angular-flask

share|improve this answer
    
thanks lukas.I think whole problem was with the project architecture.. –  user3089927 Jul 16 '14 at 23:53

Check this tutorial series that I wrote for using AngularJS and Flask together - http://tutsbucket.com/tutorials/building-a-blog-using-flask-and-angularjs-part-1/

It that tutorial, the Flask application and AngularJS is separated. The tutorial has a lot of demos on how an AngularJS app fetches data in the backend (Flask application).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.