Post Json String get NULL

I have converted all the entries of a form into an object and then fill it using the object to fill an array:

var jsonObj = [];
var formData = new FormData();
$('[name="'+form+'"] *').not(':input[type=file], :input[type=button], :input[type=submit], :input[type=reset], [name="input_search"]').filter(':input').each(function(obj, v) {
    var iObj        = {}
    var input       = $(this);
    var inputname   = input.attr("name");
    var val         = input.val();
    var inputval    = val;
    iObj[inputname] = inputval;
    jsonObj.push(iObj);
});

Later i try to convert the Final array into a Json String with this:

jsonData = JSON.stringify(jsonObj);

and i get this String:

[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]

i send this to PHP Server With this, becouse i send other Element type File with formData.append method:

formData.append('jsonData', jsonData);
var url = 'index.php';
$.ajax({
    url: url,
    data: formdata,
    contentType: false,
    processData: false,
    type: 'POST',
    cache: false,
    error: function(xhr, status, error) {
        alert(error);
    },
    success: function(response) {
        alert(response);
    }
});

In PHP server Side i try to get this string:

echo var_dump($_POST['jsonData']);

and i get this string:

string(169) "[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"

when i try to conver this to and Array with this:

Test 1:

echo '<pre>';
echo var_dump($_POST);
echo '</pre>';

ouput:

array(1) {
  ["jsonData"]=>
  string(169) "[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"
}

Test 2:

echo '<pre>';
$jsonData=$_POST['jsonData'];
$Data=json_decode($jsonData);
echo var_dump($Data);
echo '</pre>';

ouput: I get NULL.

I do not understand what I do wrong

Last Update:

i run this test:

echo '<pre>';
$jsonData   = '|'.$_POST['jsonData'].'|';
$json       = '|'.'[{"s-file[]":"Prueba 3"},{"text_file[]":"casa.jpg"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]'.'|';
echo $Line = mb_strlen(mb_strcut($jsonData, 0, strspn($jsonData ^ $json, "\0")));
echo '<br>';
echo $jsonData[$Line];
echo '<br>';
echo $json[$Line];
echo '<br>';
echo '</pre>';

Output

3
&
"

I can determine that the chain is corrupt, but I can not know why or how or how to fix it.

Answers:

Answer

Simpler way to send json is:

$.ajax({
    url: url,
    data: jsonData, // stringified JSON
    contentType: 'application/json',
   // processData: false,
    type: 'POST',
    //cache: false, POST won't cache
    error: function(xhr, status, error) {
        alert(error);
    },
    success: function(response) {
        alert(response);
    }
});

And receive in php

$data = json_decode(file_get_contents('php://input')[,boolean to force array]);

Easiest way to send data in general is to use serialize() on form and send as default content type and receive various fields as $_POST[inputName]

Answer

The problem may be related to the use of:

$_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

https://stackoverflow.com/a/9854121/963200

need work around it to solve and use after read the data from POST and GET

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.