我对 Rest API 和 Angularjs 都很陌生。我试图在用户界面上显示一条警报消息,以显示表的编辑是否成功。我想知道我们如何将警报从 Rest api Controller 发送到 angularjs? 请问有什么例子吗? 谢谢
例如,我正在尝试使用 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 Controller
@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;
}
返回消息; }
最佳答案
也许您可以返回一个带有 errorStatus 的 JSON 对象,然后您可以显示错误消息,
$scope.savePlan = function (plan,code) {
$http({
method : 'POST',
contentType: 'application/json',
url : restUrl + "plans/update/"+ plan,
data: code
}).success(function(data) {
if(data.ErrorMsg)
{
//display data.ErrorMsg
}
else
{
//successfully posted
}
}).error(function(data) {
console.log('POST ERROR: ' +data);
});
};
关于javascript - 如何显示来自 REST API Controller 的错误消息以显示在 Angularjs UI 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41818270/