Ola Zeyad
Ola Zeyad

Reputation: 113

$http.post in angularjs not work to me and $http.get has response errors

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);

    }

Upvotes: 1

Views: 676

Answers (2)

gianni
gianni

Reputation: 1347

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--,

Upvotes: 0

ojus kulkarni
ojus kulkarni

Reputation: 1907

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

Upvotes: 2

Related Questions