AngularJS $ http.get不能与node.js RESTful一起使用 [英] AngularJS $http.get won't work with node.js RESTful

查看:96
本文介绍了AngularJS $ http.get不能与node.js RESTful一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AngularJS应用程序出了问题。我为简单的GET和POST函数构建了一个Node.js RESTful API。如果我使用测试客户端发送http请求,一切正常,我得到了我的数据。
但是如果我尝试在angular-js应用程序中使用相同的请求,我会收到错误,我不知道为什么。

I have a problem with my AngularJS application. I build a Node.js RESTful API for simple GET and POST functions. If I use a test client to send the http-request, all worked fine and I get my data back. But if I try to use the same request in my angular-js application, I got an error and I don't know why.

这里是我的node.js app的代码

here is the code of my node.js app

www.js:

var app = require('../app');
var http = require('http');

var port = '3000';
app.set('port', port);
var server = http.createServer(app);
server.listen(port);

app.js:

var express = require('express');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use(bodyParser.json());

app.use('/api/v1/', routes);
app.use('/api/v1/users', users);

users.js:

router.get('/', function (req, res, next) {

    db_pool.getConnection(function (err, conn) {
        if (err) {
            conn.release();
            res.json({"code": 100, "status": "Error in connection database"});
            return;
        }

        console.log('connected as id ' + conn.threadId);
        conn.query("select * from Users", function (err, rows) {
            conn.release();
            if (!err) {
                res.setHeader('content-type', 'application/json');
                res.json(rows); // fetch rows
            } else {
                res.json({"code": 404, "status": "No Users available"});
            }
        });

        conn.on('error', function (err) {
            res.json({"code": 100, "status": "Error in connection database"});
            return;
        });
    });

});

这是我的Angular-JS代码:
app.js:

And here is my Angular-JS code: app.js:

'use strict';

angular.module('tutorialApp', [])
    .controller('UsersCtrl', function($scope, $http){
        $http.get('http://localhost:3000/api/v1/users').success(function(usersResponse) {
            $scope.users = usersResponse.data;
        }).error(function (response, status, header, config) {
            alert("ouh, an error...");
        });
    });

和HTML网站:

<script src="../js/angular.min.js"></script>
<script src="../js/modules/app.js"></script>


<div class="container">
    <input type="text" ng-model="search">

    <p ng-show="search">You searching for: {{search}}</p>

    <table class="table" ng-controller="UsersCtrl">
        <tr ng-repeat="user in users | filter:search">
            <td>{{user.user_id}}</td>
            <td>{{user.first_name}}</td>
            <td>{{user.last_name}}</td>
        </tr>
    </table>
</div>

我错了什么?

推荐答案

好吧,在我使用node.js来提供我的angular-page并将URL减少到相对URL后,一切正常。谢谢你的帮助。

Okay, after I use node.js for serving my angular-page and reduce the URL to a relative URL, all works fine. Thanks for your help.

现在我的UsersCtrl看起来像:

Now my UsersCtrl looks like:

'use strict';

angular.module('tutorialApp', [])
    .controller('UsersCtrl', function($scope, $http){
        $http.get('/api/v1/users').then(function(usersResponse) {
            $scope.users = usersResponse.data;
        })
    });

这篇关于AngularJS $ http.get不能与node.js RESTful一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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