angularjs - How to get data with my angular.js file from node.js restful api in localhost -
i created main.js rest api.when sent request local server vericek.js,response did not come localserver showing sending datas in terminal. problem. thank you
index.html
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title></title> <link href="lib/ionic/css/ionic.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> <!-- if using sass (run gulp sass first), uncomment below , remove css includes above <link href="css/ionic.app.css" rel="stylesheet"> --> <!-- ionic/angularjs js --> <script src="lib/ionic/js/ionic.bundle.js"></script> <!-- cordova script (this 404 during development) --> <script src="cordova.js"></script> <!-- app's js --> <script src="js/vericek.js"></script> </head> <body> <div ng-app="app" ng-controller="ctrl"> <button ng-click="getdata()">get data</button> <ul> <li ng-repeat="d in data">{{d.id}}</li> </ul> </div> </body> </html>
vericek.js
var app=angular.module("app",[]); app.controller("ctrl",function($scope, $http) { $scope.getdata=function() { $http.get("http://127.0.0.1:1305/listuser").success(function(response){ $scope.veriler=response.userinfo; }); } });
main.js / rest api
var express=require("express"); var app=express(); var host="127.0.0.1"; var port="1305"; var fs=require("fs"); app.get("/listuser",function(request,response){ fs.readfile(__dirname+"/"+"user.json","utf-8",function(error,data){ if(error){ response.writehead(404,{"content-type":"text/plain"}); response.end("page not found"); } else { console.log("sent data:"+data); response.writehead(200,{"content-type":"text/json"}); response.end(data); } }); }); var server=app.listen(port,host,function(){ console.log("listening port .."); });
user.json
{ "userinfo":[ { "id":"1", "username":"ali", "date":"1.1.2016", "post":"this post", "like":"2", "liked":false }, { "id":"2", "username":"veli", "date":"2.3.2015", "post":"everyting nonsense", "like":"0", "liked":false } ] }
i think using wrong ending response in main.js.
response.end() ends response process, doesn't send data.
you want use response.send(data)
see here similar question
Comments
Post a Comment