【问题标题】:angular $http post: can't get the request parametersangular $http post:无法获取请求参数
【发布时间】:2015-05-31 22:19:44
【问题描述】:

我是初学者...使用 Angular 节点和 mysql 创建一个 crud 应用程序

由于某种原因,我无法获取 console.log(request.body.name) 或 console.log(request.body.description) 之类的数据,我收到 505 错误.. 好像我只是打印 console.log('any message') 这会显示没有任何错误。

我在按钮上绑定了ng-click="editProductCategory()"

下面是editProductCategory() 方法

$scope.editProductCategory = function (){

    productCategoryService.updateProductCategory($scope.productCategoryData, productCategoryService.getIdFromUri())
        .success(function (data) {
        if(data && data.status && data.status == 'successful'){
            alert('updated successfully');
        }
        })
        .error(function (err, status) {
        console.log('********************************');
        console.log(err);
        console.log('-------------------------------');
        console.log(status);
        console.log('********************************');
        });
    }

这是我的productCategoryService.updateProductCategory

updateProductCategory: function (productCategory, productCategoryId) {
            console.log(productCategory.name);
            console.log(productCategory.description);
            console.log(productCategoryId);
            return $http.post('/updateProductCategory', {
                name: productCategory.name,
                description: productCategory.description,
                categoryId: productCategoryId
            });

        }

这里是路由配置

this.routeTable.push({
        requestType: 'post',
        requestUrl: '/updateProductCategory',
        callbackFunction: function(request, response)
        {
            console.log('/******* arrived successfully ********/');
            //console.log(request.body.description + " no errors");
            //console.log(request.param());

            response.json()
        }
    });

this.routeTable.forEach(function(route){
        if(route.requestType == 'get')
        {
            this.app.get(route.requestUrl, route.callbackFunction)
        }
        else if(route.requestType == 'post' || route.requestType == 'POST   ')
        {
            this.app.post(route.requestUrl, route.callbackFunction);
        }
        else if(route.requestType == 'delete'){}
    });

有什么想法吗?

【问题讨论】:

  • 我没有看到你的哪一行代码中断或没有按你预期的那样工作,你能指出来吗?
  • this.routeTable.push() 这个console.log(request.body.description + ' without errors') 中注释的行这些行不通

标签: javascript mysql angularjs node.js angular-http


【解决方案1】:

$http.post 会将您的数据以 JSON 格式发送,因此如果您使用 Express,则需要使用 bodyParser 中间件来解析请求正文。

var express = require('express')
var app = express.createServer();

app.use(express.bodyParser());

【讨论】:

  • 感谢@Rebornix.. 但是应用程序是使用express -e 生成的,并且它已经安装了该中间件。
猜你喜欢
  • 2019-08-05
  • 2017-12-24
  • 2018-02-18
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多