简体   繁体   中英

Error when made AngularJS $http request POST

I want to access POST method from my server, but when I logged the response, it's always return status=0, anyone can help me or give me an advise? note: I've tried my method in Postman and it's works anyway

This is my contorller

nameApp.controller('loginCtrl',  ['$scope','$location','Flash','$sessionStorage', '$cookieStore','$http',function (scope,location,Flash,sessionStorage,cookieStore,http) {

scope.nim='';
scope.pass='';
if(cookieStore.get('current')!=null){
    location.path('/homepage')
}
scope.login = function() {

    if((scope.nim=='')){
        Flash.create('danger', 'Invalid Username / Password', 'custom-class');
    }else{

        http.post('http://localhost:8084/iec3/rest/users/login',{'username':'admin','password':'bukanadmin'},{headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
               console.log("GREAT");
              }, function errorCallback(response) {
                console.log(response)
              });
    }
}
}]);

This is login method from my controller

@Path("/login")
@POST
@Consumes(value = MediaType.APPLICATION_JSON)
@Produces(value = MediaType.APPLICATION_JSON)
public Response login(User user) {
    UserDao ud = new UserDao();
    User dariDB = ud.login(user.getUsername(), user.getPassword());
    dariDB.setPassword("");
    return Response.ok(dariDB)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
            .header("Access-Control-Allow-Credentials", "true")
            .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
            .header("Access-Control-Max-Age", "1209600")
            .build();
}

This Is the error when i try to logged the response

Object {data:null, status:0, headers:dd/<(), config:Object, statusText:""}

Your request from ajax does not seem to hit your controller method, status 0 is usually returned when the request is cancelled. Check the @path value in controller method and the http://localhost:8084/iec3/rest/users/login mapping.

For more details about status 0 http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute

You need to verify if your request is reaching to the server. Is your login method is getting called? If you are on same host, there is no need to pass complete url, you can directly call your api via its path.

As for a newbie, I would recommend you to avoid $http shortcut methods. For better understanding and readability, you should use $http like

$http({
  method: 'POST',
  url: 'http://localhost:8084/iec3/rest/users/login',
  data: {
    username:'admin',
    password:'bukanadmin'
  },
  headers: {
    'Content-Type': 'application/json'
  }
}).then(function successCallback(response) {
  console.log("GREAT");
}, function errorCallback(response) {
  console.log(response)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM