【问题标题】:Cannot POST /api/无法发布 /api/
【发布时间】:2015-12-21 08:40:18
【问题描述】:

我正在尝试使用 AngularJS 和用于 NodeJS 的猫鼬模块将 Object 文件发送到服务器。我确实收到了Object,但由于Cannot POST /api/appointment/new 错误,它没有发送到服务器。

这是我的HTML 文件:

<div class="row">
        <div class="col-sm6 col-sm-offset-3">
            <div class="row">
            <strong>Date:</strong>
                <label>
                    <input type="text" type="date" ng-model="new.date" class="form-control">
                </label>
                <strong>Name Surname:</strong>
                <label>
                    <input type="text" ng-model="new.nameSurname" class="form-control">
                </label>
            </div>
            <div class="row">
                <strong>Case:</strong>
                <label>
                    <input type="text" ng-model="new.description" class="form-control">
                </label>
            <strong>Contact number:</strong>
                <label>
                    <input type="tel" ng-model="new.contactNumber" class="form-control">
                </label>
            <button ng-click='createAppointment()'>Create appointment!</button>
            </div>
        </div>
    </div>

我的 server.js 文件:

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

var app = express();
var newAppointment = require('./server/controllers/newAppointment.js');

mongoose.connect('mongodb://localhost:27017/dental');

app.use(bodyParser.json());
//noinspection JSUnresolvedVariable
app.use('/app', express.static(__dirname + '/app'));
//noinspection JSUnresolvedVariable
app.use('/node_modules', express.static(__dirname + '/node_modules'));


app.get('/', function(req, res){
    res.sendfile('index.html');
});

//New appointment function
app.post('api/appointment/new', newAppointment.create);

app.listen('3000', function() {
    console.log("Listening on :3000")
});

服务器控制器:

var mongoose;
mongoose = require('mongoose');
var appointment;
appointment = require('../datasets/appointment.js');

module.exports.create = function(req, response) {
    console.log(req.body)
};

应用控制器:

(function() {
    //noinspection JSUnresolvedFunction
    angular.module('dentalAdviser')
        .controller('appointmentController', ['$scope', '$state', '$http', function ($scope, $state, $http) {

            $scope.createAppointment = (function() {
                console.log($scope.new);
                $http.post('api/appointment/new', $scope.new).success(function (response) {
                }).error(function (error) {
                    console.log(error);
                })
            });
        }])
}());

Object 的数据集:

var mongoose = require('mongoose');
    module.export = mongoose.model('appointment', {
        nameSurname: String,
        date: String,
        description: String,
        contactNumber: String
    });

【问题讨论】:

    标签: angularjs node.js mongodb mean-stack


    【解决方案1】:

    这个:

    app.post('api/appointment/new', newAppointment.create);
    

    应该是:

    app.post('/api/appointment/new', newAppointment.create);
    

    【讨论】:

    • 我不敢相信我错过了这个......谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2019-05-10
    • 2012-04-07
    • 2020-11-01
    • 1970-01-01
    • 2023-03-28
    • 2021-06-26
    相关资源
    最近更新 更多