Error when made AngularJS $http request POST

Timotius Gunawan

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:""}

yoogeeks

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

jmeter showing error when HTTP request is made

405 Error with POST request (AngularJS $http)

Feign Client Call(to a http service) is converted from GET request to POST when the call is made in runtime

Http response error when sending a post request Angular 4

HTTP error 405 when forwarding servlet POST request

NSURLErrorDomain Code=-1001 error when a http post request is sent

HTTP post request error when trying to submit a form

Error when converting JSON HTTP POST request to Java object

Error when trying to do a HTTP Post request in Flutter

undefined index error when using $http.post in angularJS

CERT_HAS_EXPIRED error when a POST request is made through npm request module on Nodejs, Certificate is valid however

How is an HTTP POST request made in node.js?

Why is HTTP post request is made on spinner button click

Paypal with angularjs $http.post request

$http.post request not working in angularJS?

Where is data in angularjs http post request?

AngularJS update model/view on POST http request

Http POST request in AJAX works perfectly but not in AngularJS

How to intercept POST request when multiple requests are being made in Cypress?

Want to automatically insert current Date in the table When a post Request is made

How to view the address of the POST request made when an HTML button is clicked?

What HTTP request is made by browser when accessing endpoint URL?

Error occuring when calling http post request. ClientException (Failed to Parse HTTP, 67 does not match 13)

request(...).then is not a function error when making a POST request

Encounter an issue when to send post request with cookie----urllib.error.HTTPError: HTTP Error 503: Service Unavailable

Why is server getting empty request.body from POST http request made with axios?

POST http request for WIX database gives error

500 Internal error from HTTP POST request

error using nodejs to do http post request