1

Like the title said. I want to make a $http.post request to my backend in php, but is not working. Let me show you my code at today:

function obtenerSesion() { 
        return $http.post(loginUrl,
                 {params: {
                     username: 'prueba123',
                     password: 'prueba123'}
                 })
            .then(function(response) {
                debugger;
                canchas = response.data;
                return canchas;
        })
        .catch(generarError);
    }

In my PHP I have this:

.
.
22.    $nombreUsuario= $_POST["username"];
23.    $contrasenia=    $_POST["password"];
.
.

I receive in the response status "200" but error.txt catch this when the php is execute:

Undefined index on line 22

Could you tell me, Why my php is not receiving the data? Thanks!

//EDIT//

login.php

<?php
$response = array();

require("Db.class.php");
require("password.php");
require("jwt.php");

$settings = parse_ini_file("settings.ini.php");
$securityToken= '' . $settings["token"]. '';

$db = new Db();

$nombreUsuario= $_POST["username"]; //email o user
$contrasenia=   $_POST["password"];

// $hash = password_hash($contrasenia, PASSWORD_BCRYPT);


$result = $db->query("SELECT ....);

if(count($result) == 0){
    ...
    ...

If I hardcode the $nombreUsuario='prueba123' and $contrasenia='prueba123' the php works perfect!

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54
  • 1
    `params` is for query parameters (ie `$_GET`). You want `data` for `$_POST` but then you'll also want to encode it as `application/x-www-form-urlencoded` – Phil Oct 20 '16 at 23:37
  • just read row input and deserialize json, without change default content-type of angular $http.post() HTTP request – Nedev Oct 20 '16 at 23:42

1 Answers1

3

You're sending this object :

{"params":{"username":"prueba123","password":"prueba123"}}

instead of :

{"username":"prueba123","password":"prueba123"}

correct code :

function obtenerSesion() { 
    return $http.post(loginUrl,
             {
                 username: 'prueba123',
                 password: 'prueba123'
             })
        .then(function(response) {
            debugger;
            canchas = response.data;
            return canchas;
    })
    .catch(generarError);
}

and then read the raw input in PHP and deserialize the JSON like this :

$postdata = file_get_contents("php://input");

$request = json_decode($postdata);
$nombreUsuario = $request->username;
$contrasenia = $request->password;

EDIT : //

After a chat conversation This is the code that I provided him:

   function obtenerSesion() { 
    return $http({ 
    method: 'POST', 
    url: loginUrl, 
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
    transformRequest: function(obj) { 
    var str = []; 
    for(var p in obj) 
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); 
    return str.join("&"); 
    }, 
    data: {username: 'prueba123', password: 'prueba123'} 
    }).then(function successCallback(response{ 
    debugger; 
    canchas = response.data; 
    return canchas; 
    }, function errorCallback(response) { 
    //do something
    console.log(response);
    });

We changed the encode request application / x-www-form-urlencoded for its specific needs.

Nedev
  • 446
  • 4
  • 12