如何解决http baddata Angular错误?

时间:2017-07-06 09:39:39

标签: angularjs rest java-ee

我正在使用前端的Angular和后端的J2EE中的应用程序,我做了一个表单,我必须发布要保存在数据库中的数据
  邮件工作正常的问题但我不能在添加后得到服务器响应,总是我得到这个错误(奇怪的是错误来自灰色而不是红色和通常)

error screenshot

  

错误:[$ http:baddata]   http://errors.angularjs.org/1.6.4/$http/baddata?p0=%7B%22success%22%7D&p1=%7B%7D       在angular.min.js:6       在nc(angular.min.js:96)       在angular.min.js:97       在q(angular.min.js:7)       在xd(angular.min.js:97)       在f(angular.min.js:99)       在angular.min.js:134       at m。$ digest(angular.min.js:145)       at m。$ apply(angular.min.js:149)       在l(angular.min.js:102)

这里是角度代码

$scope.wflow = {
    "worcode": "HELLOoo",
    "wordest": "AVDOSS",
    "worstatus": "ACTIF",
    "worheight": 0,
    "lancode": "EN",
    "worlabel": "Salut monde",
    "wordescription": "Salut monde",
    "size": 0
};
$scope.submitForm = function () {
    console.log(JSON.stringify($scope.wflow));

    $http({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        url: host + 'api/workflow/add',
        data: $scope.wflow
    }).then(function (response) {
        console.log(response);
    }, function (response) {
        console.log(response);
    });

};

这里是Java one

@RequestMapping(value = "/add", method = RequestMethod.POST)
 @ResponseBody
 public ResponseEntity<String> addWorkflow(@RequestBody LWflow lworkflow){
  service.addWorkflow(lworkflow);
  return new ResponseEntity<String>("{\"success\"}", HttpStatus.OK);
 }

如果需要,这是html部分

          

        <table class="table">
        <tbody>
        <tr>
        <td><b>Code</b></td>
        <td><input type="text" name="worcode" class="form-control" ng-model="wflow.worcode"></td>
        </tr>
        <tr>
        <td><b>Destination</b></td>
        <td><input type="text" name="wordest" class="form-control" ng-model="wflow.wordest"><td>
        </tr>
        <tr>
        <td><b>Status</b></td>
        <td><input type="text" name="worstatus" class="form-control" ng-model="wflow.worstatus"></td>
        </tr>
        <tr>
        <td><b>Height</b></td>
        <td><input type="number" name="worheight" class="form-control" ng-model="wflow.worheight"><td>
        </tr>
        <tr>
        <td><b>Langue</b></td>
        <td><input type="text" name="lancode" class="form-control" ng-model="wflow.lancode"></td>
        </tr>
        <tr>
        <td><b>Label</b></td>
        <td><input type="text" name="worlabel" class="form-control" ng-model="wflow.worlabel"></td>
        </tr>
        <tr>
        <td><b>Description</b></td>
        <td><input type="text" name="wordescription" class="form-control" ng-model="wflow.wordescription"></td>
        </tr>
        <tr>
        <td><b>Taille</b></td>
        <td><input type="number" name="size" class="form-control" ng-model="wflow.size"></td>
        </tr>
        </tbody>
        </table>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save changes</button>
      </div>
      </form>

请注意,错误来自errorCallback函数

4 个答案:

答案 0 :(得分:2)

这是一个服务器端问题,因为响应无法通过角度读取 PS:阅读@georgeawg的最后评论

答案 1 :(得分:1)

我使用包含错误消息的对象类解决了问题,并在角度响应中捕获了正确的属性。

示例

@RequestMapping(value = "/save", method = RequestMethod.POST, produces= MediaType.APPLICATION_JSON_VALUE)
public default ResponseEntity<S> save(@RequestBody T entidade,HttpServletRequest request){
    try {
        getService().save(entidade.build());
        return new ResponseEntity(entidade, HttpStatus.OK);
    } catch (EasyQuoteServiceException e) {
        return new ResponseEntity(new Message(e.getMessage(),MessageType.ERROR), HttpStatus.BAD_REQUEST);
    }
}

我有一个名为Message的类

公共类Message实现了Serializable {

private static final long serialVersionUID = 904580124116256409L;

private String message;
private MessageType typeMessage;



public Message() {
    super();
}
public Message(String message, MessageType typeMessage) {
    super();
    this.message = message;
    this.typeMessage = typeMessage;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public MessageType getTypeMessage() {
    return typeMessage;
}
public void setTypeMessage(MessageType typeMessage) {
    this.typeMessage = typeMessage;
}

}

以及angularjs响应方法

        }).catch(function(error){
            $scope.erromessage=error.data.message;

         });
    }else{
        mensagem+="As senhas não parecem ser iguais;";
    }
    if(mensagem!=""){
        $scope.erromessage = mensagem;
    }
}

答案 2 :(得分:-1)

尝试这种方式

 $http({
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        url: host + 'api/workflow/add',
        data: angular.toJson($scope.wflow)
    }).then(function (response) {
        console.log(response);
    }, function (response) {
        console.log(response);
    });

答案 3 :(得分:-1)

这个错误可能是因为响应是在json字符串中所以angular尝试将json字符串解析为JSON。 我使用了transformResponse并解决了错误。

$http({
    url : url,
    method : 'POST',
    data: data,
    transformResponse: [
        function (data) { 
            return data; 
        }
    ]
})