$http post调用挂起

$http post call hangs

本文关键字:挂起 调用 post http      更新时间:2023-09-26

我试图创建一个页面来显示所有的论坛帖子…一切工作正常,但我注意到,当我创建一个帖子(当它重定向到显示所有的帖子)-在firebug中,帖子调用挂起了很长时间。然而,数据被发送,即使它看起来像post调用仍然悬而未决…因为它会在页面上显示所有的帖子,然后被加载。我现在就是这样做的。

post controller (post.js):

'use strict';
angular.module('appApp')
.controller('PostCtrl', function ($location, $scope, $http) {
  $scope.errors = {};
  $scope.post;
  $scope.postit = function(form) {
    $scope.submitted = true;
    if(form.$valid) {
        console.log($scope.post.posttitle + " this is $scope.post");
        $http.post('/api/post', $scope.post).success(function() {console.log("no errors whoohoo")});
        $location.path('/forum');
    }
  };
});

正如您所看到的,现在,我正在执行呼叫后的重定向($location.path)。重定向工作很好,像这样…但是,如果我试图将重定向放在success函数中:

$http.post('/api/post', $scope.post).success(function() {$location.path('/forum'); console.log("no errors whoohoo")});

重定向永远不会发生,它只是挂在post调用上。有人知道是怎么回事吗?我认为post调用应该只需要几毫秒来完成(它只是存储一个post标题和内容)。

无论我怎么做,post call都会挂起…只是第二种方式不会重定向页面。

正如在评论中建议的那样,我的服务器端代码有问题-我可能已经隔离了问题。

当我想列出页面上的所有帖子(api.js)时,这个函数会被调用:

exports.posts = function(req, res) {
  return Post.find(function (err, posts) {
    if (!err) {
      console.log(posts + " server side posts function route");
      return res.json(posts);
    } else {
      return res.send(err);
    }
  });
};

console.log消息从未出现,所以这一定是它被挂起来的地方…什么好主意吗?

调用该函数的代码区域如下所示:
app.route('/api/post')
    .get(api.posts);

事实证明,使用$location.path('/xxx')是不够的,我还需要在快速端使用res.redirect,以便重定向实际工作。