繁体   English   中英

http.get问题Angular + Node.js

[英]http.get problems angular + nodejs

当我使用此代码时,工作并得到“确定”的tem响应:

controller.js

    var myApp = angular.module('myApp',[]);

myApp.controller('AppController',['$scope','$http',function($scope,$http){

    $http.get('/').
        success(function (response) {
        console.log("ok");
    });

}]);

server.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + "/public"));

app.get('/', function (req, res){

    var person1 = {
        name: 'Tim',
        email:'tim@gmail.com',
        number:'32232233'
    };

    var person2 = {
        name :'Dani',
        email:'Dani@gmail.com',
        number:'22222222'
    };

    var contactList = [person1,person2];

    res.json = contactList;
    console.log("send work");
});

app.listen(3000);
console.log("Server running on port 3000");

但是,当我将$http.get('/')更改$http.get('/') $http.get('/list')app.get('/', function (req, res)更改为app.get('/list', function (req, res)不起作用。

获取时间“列表”永远保持等待状态 ,当我停止节点服务器时,我会得到“ GET http:// localhost:3000 / list net :: ERR_EMPTY_RESPONSE”。

您实际上并不是在调用res.json()方法。

确保您实际上是在调用方法,而不是将其分配为等于联系人列表:

res.json(contactList);

res.json = contactList

在上下文中:

app.get('/list', function (req, res){

    var person1 = {
        name: 'Tim',
        email:'tim@gmail.com',
        number:'32232233'
    };

    var person2 = {
        name :'Dani',
        email:'Dani@gmail.com',
        number:'22222222'
    };

    var contactList = [person1,person2];

    res.json(contactList);
    console.log("send work");
});

之所以可能在“ /”上获得响应是因为您的应用程序托管在“ /”上,因此您将获得响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM