javascript - 如何显示来自 REST API Controller 的错误消息以显示在 Angularjs UI 上?

标签 javascript angularjs spring rest spring-mvc

我对 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/

相关文章:

javascript - dir-paginate angularJs 创建的每个表行的行号

java - 构造函数的参数 1 需要类型为 'org.springframework.social.connect.ConnectionRepository' 的 bean,但找不到

Spring Boot Data JPA - 修改更新查询 - 刷新持久性上下文

javascript - JS 中逗号分隔的 var 的含义

javascript - 绑定(bind)另一个对象的下拉列表

javascript - 通过 WebDriver/GhostDriver 将参数传递给 PhantomJS

angularjs - 在 ionic 单元测试中广播意外的 $locationChangeStart 和 $locationChangeSuccess 事件

java - 在maven多模块项目中,如何从一个模块访问另一个模块的spring bean

javascript - IE和其他浏览器的图像质量差异

javascript - http javaScript 将属性默认设置为所有标签