#
#
#FFFFFF2018-07-24 10:28:05
PHP
#FFFFFF, 2018-07-24 10:28:05

How to send JSON with nested objects/arrays via Ajax and parse it on the server in PHP?

Hello.
I can't figure out how to transfer my object to the server and parse it there. View object:

var data = {data: {
          name: '',
          birth_date: '',
          family_status: '',
          citizenship: '',
          country: '',
          area: '',
          register: {
            city: '',
            street: '',
            building: '',
            flat: ''
          },
          live: {
            reg: '',
            city: '',
            street: '',
            building: '',
            flat: '',
            flat_type: 'в личной квартире'
          },
          home_phone: '',
          mob_phone: '',
          skype: '',
          email: '',
          no_relatives: false,
          relatives: [
            {
              name: '',
              status: 'мать',
              birth_year: '',
              address: '',
              position: '',
              work: ''
            }
          ]
}}

I send using the POST method:
axios.post('demo.ru/ajax.php', data, {
                  headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                  }
                })

I'm trying to view on the server:
<?
if (isset($_POST)) {
    $decodedArray = json_decode($_POST,true);
}
var_dump($decodedArray);
?>

But nothing is output. As I understand it, the problem is in the form of the data that I send.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Vanin, 2018-07-24
@victory_vas

<?php
if (isset($_POST)) {
    $json = file_get_contents('php://input');
    $decodedArray = json_decode($json, true);
}
var_dump($decodedArray);
?>

F
frees2, 2018-07-24
@frees2

<?php 
//print_r($_POST);
if (!empty($_POST[json_name])) { $json_data = $_POST['json_name'];$json = json_decode($json_data,true); $a = $json['name'];$b = $json['idtime'];
 if ($a !== false) {

var data = data: {
          name: '',
------------------
json_name="+data ;

It is necessary to prescribe such a figovinka before the list, with any name
json_name =

B
Brother79Mail, 2018-08-11
@Brother79Mail

Why decode on the PHP side at all? Try to write print_r($_POST) on the PHP side, judging by the code, your structure should be there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question