如何从localhost中的node.js restful api获取我的angular.js文件的数据 [英] How to get data with my angular.js file from node.js restful api in localhost

查看:113
本文介绍了如何从localhost中的node.js restful api获取我的angular.js文件的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为rest api创建了main.js.当我从vericek.js向本地服务器发送请求时,响应没有返回,但是localserver显示在终端中发送数据。
有什么问题。
谢谢

I created main.js for rest api.When I sent request to local server from vericek.js,response did not come back but localserver is showing sending datas in terminal. What is the problem. Thank you

<!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), then uncomment below and remove the 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 will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your 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



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



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



user.json

{
    "userInfo":[
        {
            "id":"1",
            "username":"ali",
            "date":"1.1.2016",
            "post":"this is a post",
            "like":"2",
            "liked":false
        },
        {
            "id":"2",
            "username":"veli",
            "date":"2.3.2015",
            "post":"Everyting nonsense",
            "like":"0",
            "liked":false
        }
    ]
}


推荐答案

我认为你在main.js中的回答使用了错误的结尾。

I think you are using the wrong ending to your response in main.js.

response.end()只是结束响应过程,它不会发送任何数据。

response.end() just ends the response process, it doesn't send any data.

你想要使用response.send(data)

You want to use response.send(data)

在此处查看类似问题

这篇关于如何从localhost中的node.js restful api获取我的angular.js文件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆