0

I'm implementing a JWT authenfication in AngularJS for front-end and PHP for back-end, and I just set the header authorization :

.run(['$http', function($http) {
$http.defaults.headers.common['Authorization'] =  'Bearer '+localStorage.getItem('tokenAPI');
}])

Since i added this lines , no $_POST variable pass in my POST request

Here is how i send this variables :

let body = new URLSearchParams();
body.set('username', $scope.username);
body.set('password', $scope.pwd);
body.set('project', $rootScope.project);

//Request options
var req = {
  method: 'POST',
  url: Global.url_api+'action=Connection',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data: body.toString()
}

And here how i receive :

$project = $_POST['project'];
$username = $_POST['username'];
$password = $_POST['password'];

Another thing since i added this header, my request method became an Option method not POST anymore.

  • better implement your `Authorization` using http interceptor, you can look at here https://docs.angularjs.org/api/ng/service/$http#interceptors – John Velasquez May 28 '18 at 15:13
  • I had issues with Angular sending data via POST but it was showing up in the input vs the $_POST array. Try `$dataSent=file_get_contents("php://input");` – ivanivan May 28 '18 at 15:24
  • The CORS spec requires the OPTIONS call to precede the POST if it has any non-simple content or headers in it. See [MDN Web API Reference - CORS Preflighted requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests) – georgeawg May 28 '18 at 16:45

0 Answers0