使用POST方法发送时出现JSON格式错误

时间:2016-09-21 17:29:29

标签: angularjs mongodb api express

我有"格式"在我的json帖子中

我有像HTML-Angular这样的表格

<form name="signupForm" ng-submit="processForm()">
    <input name="names" ng-model="userInfo.names" type="text" class="form-control" required>
    <input name="lastName" ng-model="userInfo.lastName"  type="text" class="form-control" required>
    <input name="city" ng-model="userInfo.personals.city" type="text" class="form-control" required>
    <input name="cellNumber" ng-model= "userInfo.personals.cellNumber" type="text" required>
    <select multiple chosen class="chosen-select" ng-model="userInfo.personals.area">
        <option value="1">OP1</option>
        <option value="2">OP2</option>
        <option value="3">OP3</option>
    </select>
    <button type="submit" class="btn btn-success" ng-disabled="signupForm.$invalid">Post</button>
</form>

我的控制器是

function mainCTRL ($scope, $rootScope, $timeout, notify, $http) {
    $scope.processForm = function() {
        $http.post('/register', $scope.userInfo).success(function(res){console.log(res);});
    };
}

我的问题是当我的api获得JSON时,它看起来像

{
    names: "name",
    lastname: "last",
    personals: {
        city: "city",
        cellNumber: "12345",
        area: ['1','2']
    }
}

我尝试将其保存在模型中

router.post('/register', function(req, res, next){
    console.log(req.body);
    var perfil = new Perfiles(req.body);
    perfil.save(function(err, perfil){
         if(err){return next(err);}
            res.json(perfil);
    });
});

但是它显示了500错误。如果我使用Postman发送帖子,请求格式不同,可以保存

{
        names: "name",
        lastname: "last",
        personals.city: "city",
        personals.cellNumber: "12345",
        personals.area: ['1','2']
        }
  }

enter image description here

两种方法之间的区别是什么?

1 个答案:

答案 0 :(得分:-1)

如您所知,请求有两种数据类型:表单类型和json类型。

因此,您需要在发布时确定请求数据的类型。

$http({
    data: $scope.userInfo,
    method: "POST",
    url: "/register",
    headers: {
        'content-type': "application/x-www-form-urlencoded"
    },
    dataType: 'JSON',
}).then(function mySucces(res) {
   console.log(res);
});

Postman的结果与网站不同的原因只是你错过了POSTMAN的数据类型选择。

请尝试选择以下类型并再次发送。 enter image description here

提交时,您可以从html表单中获取json值。

<form name="signupForm" ng-submit="processForm(userInfo)">
    <input name="names" ng-model="userInfo.names" type="text" class="form-control" required>
    <input name="lastName" ng-model="userInfo.lastName"  type="text" class="form-control" required>
    <input name="city" ng-model="userInfo.personals.city" type="text" class="form-control" required>
    <input name="cellNumber" ng-model= "userInfo.personals.cellNumber" type="text" required>
    <select multiple chosen class="chosen-select" ng-model="userInfo.personals.area">
        <option value="1">OP1</option>
        <option value="2">OP2</option>
        <option value="3">OP3</option>
    </select>
    <button type="submit" class="btn btn-success" ng-disabled="signupForm.$invalid">Post</button>
</form>

...

$scope.processForm = function(userInfo) {
  console.log(userinfo);
}