0

I know there are several posts on SO and on many blogs explaining how to get the JSON data that was sent to be read by php. Some use url-encoding, others file_get_contents.

Anyway, it doesn't work for me. I'm sure there is ridiculously easy explanation for it but my code simply doesn't work (the request is sent, the backend answers but that message is more or less always the same: nothing seems to arrive there !

So in my controller I have :

var request_data = {
    firstname: 'Quentin',
    lastname: 'Hapsburg'
  };

$http.post("lib/api/public/blog/post", JSON.stringify(request_data))
    .success(function(data) {
        console.log(data);
    })

And in the php file:

$data = file_get_contents("php://input");
$data = json_decode($data, TRUE);

var_dump($data);

The result is NULL ! Any suggestions on where my error is ???

EDIT: This might have something to do with the rewriting rules but is not a duplicate since the same answer does not solve this question !

YannickHelmut
  • 555
  • 1
  • 7
  • 20
  • Start by looking at your browser's debugger tools' network inspector and figuring out the exact data being sent to PHP and what the response is. Perhaps your server is replying with a 302 redirect, which would discard the POST data… – deceze Sep 05 '16 at 11:01
  • I checked that and unfortunately I don't have any specific errors. – YannickHelmut Sep 05 '16 at 11:07
  • what kind of url rewriting are you using for this php? slim, nginx, apache? – lyoko Sep 05 '16 at 11:19
  • Try not stringifying it, but sending it as actual JSON object – casraf Sep 05 '16 at 11:35
  • RewriteEngine On RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] RewriteRule ^ /app/index.html – YannickHelmut Sep 05 '16 at 11:57
  • Could the rewriting rules be causing that problem ? – YannickHelmut Sep 05 '16 at 11:58
  • Possible duplicate of [AngularJs $http.post() does not send data](http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data) – RIYAJ KHAN Sep 05 '16 at 12:10

2 Answers2

0

try using application/x-www-form-urlencoded

$http.post(urlBase, $httpParamSerializer(object), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
}).then(function(r) {
    callback(r);
});

and then php will be able to access to your posted object using $_POST

lyoko
  • 429
  • 3
  • 4
-1

Please use this js file. It will work

Index.html:

<html>
<head>
   <script src="js/angular.min.js"></script>
</head>

<body ng-app="myApp">
<div ng-controller="mainCtrl">
    sdadasdasd
</div>
<script src="app.js"></script>
</body>
</html>

app.js:

var app = angular.module("myApp", []);
app.controller("mainCtrl", function($scope,$http){
    var request_data = {
        firstname: 'Quentin',
        lastname: 'Hapsburg'
    };

    $http.post("test.php", JSON.stringify(request_data)).success(function(data) {
        console.log(data);
    });
});
Raj
  • 439
  • 1
  • 8
  • 28