Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post JSON from angular 2 to php

I try to post data from angular 2 to php:

let headers = new Headers();
headers.append('Content-Type', 'application/json');
var order = {'order': this.orders};

this.http.post('http://myserver/processorder.php', JSON.stringify(order), {
    headers: headers
}).subscribe(res => {
    console.log('post result %o', res);
});

In angular 2 one can only post string as data and not Json? That's ok for me but I struggle to get the posted data on php. I tried $obj = $_POST['order'];

like image 917
daniel Avatar asked Feb 10 '16 21:02

daniel


People also ask

How to receive POST JSON data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

How to get a JSON object in PHP?

PHP File explained:Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.

How to get JSON object from string in PHP?

To convert a valid JSON string back, you can use the json_decode() method. To convert it back to an object use this method: $jObj = json_decode($jsonString);


3 Answers

Marc B is correct, however what is happening is that the $_POST array will contain an empty value with a key set to the JSON string you are passing...

Array
(
    [{"order":"foobar"}] => 
)

You "can" grab that (although this would be the wrong approach) by getting the key using...

key($_POST)

for example:

$obj = json_decode(key($_POST));
echo $obj->order;

BUT what you can do is send the data as value key pairs:

let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
let order = 'order=foobar';

this.http.post('http://myserver/processorder.php', order, {
    headers: headers
}).subscribe(res => {
    console.log('post result %o', res);
});

Then in PHP you can grab the data using:

$_POST['order']

Few things to note:

  • changed header Content-Type to application/x-www-form-urlencoded (more for my own testing since this doesn't do any preflight requests)
  • notice that order is a key value pair string instead of JSON
  • notice that order in this.http.post is getting passed as-is without JSON.stringify
like image 132
inki Avatar answered Sep 21 '22 18:09

inki


I do not know if it's bad practice but it seems right to me, although it bothers me a little

const headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

const obj = { nome: 'gabriel', age: 20 };
const body = 'data=' + JSON.stringify(obj);

this.http.post('/test.php', body, { headers })
  .subscribe(res => console.log(res.json()), res => console.error(res))

And in php

$post = json_decode($_POST['data']);
like image 21
Gabriel Martins Avatar answered Sep 18 '22 18:09

Gabriel Martins


Agreed with you that we can't at the moment provide object instead of string. It's a feature in progress. See this issue:

  • https://github.com/angular/http/issues/69

Regarding your problem to get JSON data on the server side, this question should help you:

  • Read associative array from json in $_POST
like image 40
Thierry Templier Avatar answered Sep 18 '22 18:09

Thierry Templier