简体   繁体   中英

PHP get INT from angular http request

How to get an int-value from an $http-request in PHP ?

I´m trying to send a http request from an Angular app but i´m not able to get the value of the sent `id`` parameter into my PHP variable $id.

I tried several methods like:

$id = $_POST['id'];

or

$id = $connection->real_escape_string(isset($_POST['id']) ? $_POST['id'] :'');

in my PHP but nothing works...

This is my function to send the $http request

function removeFriend( id ) {
        var request = $http({
            method: "delete",
            url: "php/deleteFriend.php",data: $.param({'id':id}),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        });
        return( request.then( handleSuccess, handleError ) );
    }

The problem was the method of the $http request. you have to use POST so PHP can get the values with the $_POST['id'] method.

So here is the code that works:

Javascript

function removeFriend( id ) {
        var request = $http({
            method: "post",
            url: "php/deleteFriend.php",data: $.param({'id':id}),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        });
        return( request.then( handleSuccess, handleError ) );
    }

PHP

$id = $_POST['id'];

I think you need to make your request like this :

function removeAccount(id) {
  var request = $http({
    method: "DELETE",
    url: urlBase,
    data: {
      id: id
    }
  });

  return (request.then(handleSuccess, handleError));

}

also you please add at your config angular this code :

$httpProvider.defaults.transformRequest = function(data) {
        if (data === undefined) {
            return data;
        }
        return $.param(data);
    };
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

Mine working and hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM