1

I am new to angularjs am tying to learn it but some problems faced me, actually they are two problems:

First Problem: $http.post never works as there is no action and there is no response. However, $http.get is able to work.

Second Problem: Because of the first problem I call my restful webservice by $http.get, but the web service response status always is -1. Though the web service is able to do its work successfully and always response data null, can any one help me.

this my angular part:

var app = angular.module('myLogin',[]);
                  app.controller('loginController',function($scope,$http){ 
                  $scope.login=function(){ 
                    var username = $scope.username;
                    var password = $scope.pass;

                    $http.get("http://localhost:8080/spring/webservice/login/"+username+"/"+password)

                   .success(function(data,status){
                    alert("data : "+data);
                      alert("Data Inserted Successfully");
                    window.location.href = "chatScreen.html"; 

                  })
                  .error(function(data,status){
                      alert("Status: "+status);
                      window.location.href = "login.html";    

                  });

                      }
                 });

and this my web service:

/**
     * web service part
     */
    @RequestMapping(value="webservice/login/{name}/{pass}", method=RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<String> weblogin(@PathVariable("name") String name, @PathVariable("pass") String pass)
    {
        System.out.print("username : "+name);
        System.out.print(pass);
        UserService service = new UserService();
        List<UserBean> users = service.getUsers();
        if(users!=null)
        {
            for(UserBean user : users)
                if( ( user.getUsername().equals(name) ) && ( user.getPassword().equals(pass)  ) )
                {
                    System.out.print("success");

                    username = name;
                    //model.addAttribute("result", "Welcome to chat..");                    

                    MessageService messageService = new MessageService();
                    List<MessageBean> messages = messageService.getMessage(username);
                    String userMessages="";
                    if(messages != null)
                    {
                        for(MessageBean msg : messages) 
                            userMessages +="\n"+msg.getSender() + ": " + msg.getMessage()+" \n";
                    }
                    else
                        userMessages +="You have no Messages !";

                    //model.addAttribute("whoSendToMe", userMessages);

                    return new ResponseEntity(HttpStatus.OK);
                }
        }

        return new ResponseEntity<String>("faild", HttpStatus.NOT_FOUND);

    }
3
  • post your code, please Commented Jul 4, 2016 at 23:04
  • did you tested your service with another tool to see if it can receive requests? Commented Jul 5, 2016 at 4:51
  • no i didn,t test it Commented Jul 5, 2016 at 5:24

2 Answers 2

2

refer this may be this will give you idea how to approach your problem:-

$http({
 method: 'GET',
 url: '/someUrl'
}).then(function successCallback(response) {
   // this is asynchronous call back
  // you will get your data here comming from rest
}, function errorCallback(response) {
  // called asynchronously if an error occurs
});

share your code so we will try to solve it

Sign up to request clarification or add additional context in comments.

2 Comments

it always call errorCallback function i dont know where is the error
it may be possible URL is wrong so it is calling error or may be method of $http is not same as REST method. and I will suggest you if you dont want any tricky data then use .factory or .service method
0

If you use method GET and you receive a -1 returned, it means normally that you are giving a wrong URL.

As for then POST method you should use this syntax:

return $http({
            method: 'POST',
            url: 'index.php/email/createDeliverable',
            data: $.param({
                csrfTokenName: --your token--,
                userName: user.name,
                password: password
            }),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });

Remember to add the headers part.

Your server may need a CSRF token validation, in this case you need to pass it, see un my example: csrfTokenName: --your token--,

2 Comments

how is it possible that the URL is wrong while it reaches the web service and insert the data in the database
In the project I am working on, I receiver a -1 from a service returning a json file, where I gave the wrong address. I guess it may be the same when the service is not really returning a value. You probably have a service for writing in DB, and a separate one for reading?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.