使用AngularJS $ http.post表达服务器状态404错误

时间:2017-08-29 16:04:09

标签: angularjs node.js express

所以我为我的快速服务器和角度前端设置了单独的端口,这是第一次使用yeoman和grunt。这也是我第一次尝试使用这种客户端/服务器结构的Web项目,而且我已经碰壁了。

我的前端有一个注册表格在localhost:9000 /注册下,当我点击提交时,我得到以下回复

{firstname: "test", lastname: "test", email: "test", password1: "test", password2: "test"}
angular.js:12759 POST http://localhost:9000/signup 404 (Not Found)

(anonymous) @ angular.js:12759
sendReq @ angular.js:12492
serverRequest @ angular.js:12244
processQueue @ angular.js:17051
(anonymous) @ angular.js:17095
$digest @ angular.js:18233
$apply @ angular.js:18531
(anonymous) @ angular.js:27346
dispatch @ jquery.js:5206
elemData.handle @ jquery.js:5014
angular.js:14700 Possibly unhandled rejection: {"data":"Cannot POST     /signup\n","status":404,"config":
{"method":"POST","transformRequest":        
[null],"transformResponse:[null],"jsonpCallbackParam":"callback","url":"/signup","data":
{"firstname":"test","lastname":"test","email":"test","password1":"test",
"password2":"test"},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}}
,"statusText":"Not Found","xhrStatus":"complete"}

第一行是我将表单的结果打印回浏览器控制台。第二个是404 on / signup,我不确定这是快递路线还是客户/注册?

这是我的signup.js,位于快递

中的/ routes文件夹中
// Include Express
var express = require('express');
// Initialize the Router
var router = express.Router();

// Setup the Route
router.post('/', function (req, res) {

// show the request body in the command line
console.log(req.body);

// return a json response to angular
res.json({
    'msg': 'success!'
});
});

// Expose the module
module.exports = router;

我已将它包含在我的app.js文件中,快递用

var signup = require('./routes/signup');
...
app.use('/signup', signup);

Abd最后是我的signup.js文件夹,它是/ signup表单的控制器。

'use strict';

angular.module('clientApp') // make sure this is set to whatever it is in your client/scripts/app.js
  .controller('SignupCtrl', function ($scope, $http) { // note the added $http depedency

    // Here we're creating some local references
    // so that we don't have to type $scope every
    // damn time
    var user,
        signup;

    // Here we're creating a scope for our Signup page.
    // This will hold our data and methods for this page.
    $scope.signup = signup = {};

    // In our signup.html, we'll be using the ng-model
    // attribute to populate this object.
    signup.user = user = {};

    // This is our method that will post to our server.
    signup.submit = function () {

      // make sure all fields are filled out...
      // aren't you glad you're not typing out
      // $scope.signup.user.firstname everytime now??
      if (
        !user.firstname ||
        !user.lastname ||
        !user.email ||
        !user.password1 ||
        !user.password2
      ) {
        alert('Please fill out all form fields.');
        return false;
      }

      // make sure the passwords match match
      if (user.password1 !== user.password2) {
        alert('Your passwords must match.');
        return false;
      }

      // Just so we can confirm that the bindings are working
      console.log(user);


    $http.post('/signup', user)
    .then(function(response) {
      console.log(response.data);
  });
}
  });

我正式感到难过,并且非常喜欢一些见解 - 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

问题是您要发布到localhost:9000 / signup,这是前端,但前端不支持该路由的post方法。您需要将请求发送到localhost:{YOUR_EXPRESS_SERVER_PORT} /注册。

改变这个 $http.post('/signup', user)$http.post('http://localhost:{YOUR_EXPRESS_SERVER_PORT}/signup', user)