【问题标题】:Angular Put Method角度放置方法
【发布时间】:2016-03-23 14:22:30
【问题描述】:

我正在尝试使用 $http.put 进行 Angular 更新。当我执行 console.log 时,数据成功地从表单传递到客户端控制器,但是当我执行 $http.put 请求时。它返回为 PUT http://localhost:3000/articles 500(内部服务器错误)。

这是客户端articles.controller.js:

$scope.updateArticle = function(){
    var data = {
        id:         $routeParams.id, 
        title:      $scope.article.title, 
        body:       $scope.article.body, 
        category:   $scope.article.category
    }
    console.log(data);

    $http.put('/articles', data).success(function(data, status){
        console.log('abc');

    });



    $location.path('/articles');


}

这是articles.js 的服务器端路由:

router.put('/', function(req, res, next){
    var id = req.body.id;

    var data = {
        title: req.body.title, 
        category: req.body.category, 
        body: req.body.body
    };

    Article.updateArticle(id, data, function(err, article){
        if(err){
            console.log(err);
        }

        res.location('/articles');
        res.redirect('/articles');
    }); 
});

这是模型article.js:

module.exports.updateArticle = function(id, data, callback){
    var title = data.title; 
    var body = data.body; 
    var category = data.category; 

    var query = {_id: id};

    Article.findById(id, function(err, article){
        if(!article){
            return next(new Error('Could not load article'));
        } else {
            article.title = title; 
            article.body = body; 
            article.category = category; 

            article.save(callback);
        }
    })
}

【问题讨论】:

  • 您正在对/articles 进行放置,但在您的articles.js 路径中我看到router.put('/'。你有router.put('/articles')吗?
  • Internal Server Error 表示您的服务器发生异常。你在做 updateArticle, Article.findById 也许Articleundefined..?

标签: angularjs node.js mean-stack


【解决方案1】:

为什么不尝试直接创建一个以“/articles”为目标的 router.put 路由呢?您的articles.js 文件应如下所示:

router.put('/articles', function(req, res, next){
    console.log("Hit my route!");
    //etc... 
}); 

【讨论】:

    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多