简体   繁体   English

angular $ http post:无法获取请求参数

[英]angular $http post: can't get the request parameters

I'm a beginner... creating a crud application with angular node and mysql 我是一个初学者...用角节点和mysql创建一个Crud应用程序

for some reason I can't get the data like console.log(request.body.name) or console.log(request.body.description) I get 505 error.. where as if I simply print console.log('any message') this gets displayed without any error. 由于某种原因,我无法获得console.log(request.body.name)console.log(request.body.description)我收到505错误。。好像我只是在打印console.log('any message')console.log('any message')将正确显示。

I've this bind ng-click="editProductCategory()" on a button 我在按钮上绑定了ng-click="editProductCategory()"

bewlow is editProductCategory() method 下面是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('********************************');
        });
    }

and here is my productCategoryService.updateProductCategory 这是我的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
            });

        }

Here is the route configuration 这是路由配置

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'){}
    });

any idea? 任何想法?

$http.post will send your data as JSON, so if you are using Express, you need use the bodyParser middleware to parse the request body. $http.post将以JSON $http.post发送数据,因此,如果使用Express,则需要使用bodyParser中间件来解析请求正文。

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

app.use(express.bodyParser());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM