在AngularJS中发布嵌套JSON [400错误]

时间:2015-07-08 10:45:12

标签: json angularjs http-status-code-400

我的部分HTML

    <div>House Details:
        <input type ="number" ng-model="house.totalArea" placeholder="total_area">
        <input type="number" ng-model="house.cost" placeholder="Cost">    
        </div>

        <div>Address:
        <input type="text" ng-model="house.address.state" placeholder="state">
        <input type="text" ng-model="house.address.city" placeholder="city ">
  </div>

角:$scope.house = {}; $scope.house.address = {}; $scope.processRentForm = function () { console.log($scope.house); $http.post("http://localhost:8080/Property101/house/addHouse", $scope). error(function (data, status, headers, config) {alert("Submit failed!!");
代码适用于$ scope.house但

对于嵌套的JSON,我得到400错误(语法上不正确),正在形成

Object {address: Object, totalArea: 1000}

我在服务器端的House类中有一个Address类,它具有与我正在尝试发送的JSON类似的no./names参数。

SO的一些示例有“house.address.state”语法。 我是新角色,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在将请求JSON发送到服务器端之前,请执行以下两个步骤,

1)var reqObj = JSON.stringify($ scope.house);

2)var requestJson = JSON.parse(reqObj)

现在发送requestJson作为请求。它应该工作。