我是Rest API和Angularjs的新手。我试图在用户界面上显示一条警告消息,以显示我的表格编辑是否成功。我想知道我们怎么能把Rest api控制器的警报发送到angularjs? 请举例吗? 感谢如何显示来自REST API控制器的错误消息以显示在Angularjs UI上?
例如我试图使用angularjs HTTP
$scope.savePlan = function (plan,code) {
$http({
method : 'POST',
contentType: 'application/json',
url : restUrl + "plans/update/"+ plan,
data: code
}).success(function(data) {
console.log('POST: ' + data);
}).error(function(data) {
console.log('POST ERROR: ' +data);
});
};
REST API控制器
@RequestMapping(value = "/update/{plan}", consumes = MediaType
.APPLICATION_JSON,
method = RequestMethod.POST)
public String savePlan(@PathVariable("plan") String plan,
@RequestBody String code) throws Exception {
String message="";
if (code== null || code.trim().length() == 0 ||
plan== null || plan.trim().length() == 0)
{
//instead of logger.error i want to send that error message to display on the UI
message = "Plan details and type must be provided." +null;
}
返回消息张贴的东西; }
你可以在你的API,而不是使用返回错误信息'void' –
发[ResponseEntity(http://docs.spring.io/spring-framework/ docs/current/javadoc-api/org/springframework/http/ResponseEntity.html)与[HttpStatus](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/ http/HttpStatus.html)[BAD_REQUEST](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpStatus.html#BAD_REQUEST)和一条消息。 – georgeawg