Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm new to both Rest API and Angularjs. I'm trying to display an alert message on the UI to show if the edit of my table has been succeeded or not. I wonder how could we send the alert from Rest api controller to angularjs? Any example please? Thanks

for example i'm trying to post something using 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;

    }

return message ; }

share|improve this question
    
You can return the error message in your api instead of using void – Hung Cao Jan 24 at 0:55
    
Send a ResponseEntity with a HttpStatus of BAD_REQUEST and a message. – georgeawg Jan 24 at 17:33
up vote 0 down vote accepted

Probably you could return a JSON object with errorStatus and then you can display the error message,

$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);
        });
      };
share|improve this answer
    
hi, if my REST API controller return a string message. what should i put in that? like how I edit my question above. thanks – RLe Jan 24 at 0:57
    
put the error message there and display the data. you might have to hardcode and check the string if its an error – Sajeetharan Jan 24 at 0:59

Using rest full api your controller which is on server side should return either error or success with response(json format). 200 is the code of success, which should be returned as a status along with the response from server.

In angular, your success block is not meant to handle errors. Only if 200 which is 'OK'(indicates success) will fall in your success block, all other status will fall in error block.

For error popup you can use bootbox

share|improve this answer
    
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. For more information, see AngularJS $http Service API Reference - General Usage. – georgeawg Jan 24 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.