ImpostorIsAsking
ImpostorIsAsking

Reputation: 73

Server Validation in angularjs

i'm using ruby on rails in my backend and angularjs in my client side.

here's a scenario i input same email in my form so in my console response a 422 (Unprocessable Entity) and said 'email was use'. it means my validation was good and working properly in my backend.

my question is how can u get that response and put that in my view so user can see that email was already in use.

Upvotes: 1

Views: 35

Answers (1)

Daniel Thompson
Daniel Thompson

Reputation: 2351

If you are using angular's $http service, getting the status code of the response is simple, just access res.status from one of your factories/services:

$http.get(`/route/${id}/foo.json`).then(function(res){
    callback(res)
}).catch( (error) => { console.log(error) })

If you want to get that into a controller, I suggest using a callback function like this:

someFactory.get($scope.id, function(data){
    console.log("data.status");
})

Upvotes: 1

Related Questions