0

AngularJS $http.post() isn't sending data as I expect it to. I try to pass a range of data to a PHP script stored on the same server and when I attempt to echo this back the data always seems to be empty.

My PHP script receiving the data

<?php

$char = $_POST["char"];
$alpha = $_POST["alpha"];

echo $char;

My JS posting to the PHP script

.controller("IndexController", function($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

    $http.post("http://jxxxxxxx.xxx/ui/php/curl/index.php", {
        char: "a", alpha: 1
    }).then(function(response) {
        console.log(response);
    });
});

In the above instance, response should contain a data property containing the value of $char, in this instance "a".

However, if I

echo "hey"; 

out of the PHP script, it receives this as the data property when I log the response to the console.

Any idea why my PHP script isn't receiving the POST values correctly?

Jack hardcastle
  • 2,748
  • 4
  • 22
  • 40
  • possible duplicate of [Angular HTTP post to PHP and undefined](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined) – Marko Gresak Aug 11 '15 at 09:20
  • this should help you solve your problem https://codeforgeek.com/2014/07/angular-post-request-php/ – stackg91 Aug 11 '15 at 12:13

0 Answers0