Monday, September 12, 2016

angularjs 1.5 / laravel bad request 400

Leave a Comment

When I do a POST request using my angular app to my laravel api I get an error bad request 400 , however GET requests work normally, also I do POST requests using POSTMAN and it works , here is my code :

$http.get('http://localhost/laravel/datingApp/jobapi/public/api/user/users'); 

this get request works, this post request doesn't (data is an js object {'email': 'value','password': 'value'}) :

  $http({              url: 'http://localhost/laravel/datingApp/jobapi/public/api/user/login',             method: 'POST',             contentType: "application/json",             data: data    }).success(function(data) {              console.log(data);    }); 

also I tried doing that just regular way: $http.post('blabla'); but it doesn't work either :(

edit: sorry didn't include error:

angular.js:12011 POST    http://localhost/laravel/datingApp/jobapi/public/api/user/login 400 (Bad Request)  (anonymous function)          @ angular.js:12011 sendReq                       @ angular.js:11776 serverRequest                 @ angular.js:11571 processQueue                  @ angular.js:16383 (anonymous function)          @ angular.js:16399 $eval                         @ angular.js:17682 $digest                       @ angular.js:17495 $apply                        @ angular.js:17790 (anonymous function)          @ angular.js:25890 dispatch                      @ jquery.min.js:3 q.handle                      @ jquery.min.js:3 

2 Answers

Answers 1

That was a stupid error, but thanks to @piscator I have found it, Laravel by default returns 400 when validation rules are not met , So my input was wrong all the time that is the reason I got 400.

Answers 2

If the post request is working via postman, it seems that when your angular app does an post request it gets stuck due to browser CORS policy.

You need to have a access-control-allow-origin present in the header of the response for the post requests to works.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment