【问题标题】:http.get problems angular + nodejshttp.get 问题 angular + nodejs
【发布时间】:2015-04-09 21:39:43
【问题描述】:

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

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('/list') 并将app.get('/', function (req, res) 更改为app.get('/list', function (req, res) 时不起作用。

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

【问题讨论】:

  • res.json(contactList);

标签: javascript angularjs node.js http


【解决方案1】:

您实际上并没有调用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");
});

您可能会在“/”上收到响应的原因是因为您的应用托管在“/”上,因此您会收到响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2016-03-19
    • 2013-05-14
    • 2020-05-29
    • 2015-10-19
    • 2017-03-15
    • 2016-12-10
    相关资源
    最近更新 更多