0

I'm actually coding a little authentification service in angular for front side and php for server side.

When I post identification to the server for check identification the server responds with a boolean, true if ID are correct or false if isn't.

When I try my function with POSTMAN the response is good (true for good ID , false for wrong ID) BUT when I use it with angular with EXACTLY the same argument the server always response FALSE

Here is the code :

Server side :

else if($action == 'Connection'){
    try{
        $project = $_POST['project'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $stmt = $bdd->prepare("SELECT * FROM Projects WHERE name = :name AND admin_user = :username AND admin_pwd = :password");
        $stmt->bindParam(":name", $project);
        $stmt->bindParam(":username", $username);
        $stmt->bindParam(":password", $password);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if(count($result) === 0) {$response->credentialsOk = false;}
        else {$response->credentialsOk = true;}
        echo(json_encode($response));
    }catch(Exception $e){
        die('Erreur : '.$e->getMessage());
    }
}

Client side :

$rootScope.Connect = () => { // === $rootScope.Connect = function() { ... }
       //Ask to the server if credentials is correct
       $http.post(Global.url_api+'action=Connection',{username : $scope.username , password : $scope.pwd, project : $rootScope.project })
       .then((response) => {
         console.log(response);
       })
     }

Result

postman

client result

Yes I used exactly the same ID

Please guide me on the good way

Thanks in advance

Luuklag
  • 3,897
  • 11
  • 38
  • 57

1 Answers1

0

You are reading from $_POST so you are expecting the data to be sent encoded as either application/x-www-form-urlencoded or multipart/form-data.

Angular defaults to sending data encoded as application/json.

Either change the format you are sending data in or change the format you expect to receive data in.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335