发布Json String获取NULL [英] 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);
});
稍后,我尝试使用以下命令将Final数组转换为Json字符串:
Later i try to convert the Final array into a Json String with this:
jsonData = JSON.stringify(jsonObj);
我得到这个字符串:
[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]
我将此发送到PHP Server,因为我使用formData.append
方法发送了其他元素类型文件:
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);
}
});
在PHP服务器端,我尝试获取此字符串:
In PHP server Side i try to get this string:
echo var_dump($_POST['jsonData']);
我得到这个字符串:
string(169) "[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"
当我尝试将其转换为Array时:
when i try to conver this to and Array with this:
测试1:
echo '<pre>';
echo var_dump($_POST);
echo '</pre>';
输出:
array(1) {
["jsonData"]=>
string(169) "[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"
}
测试2:
echo '<pre>';
$jsonData=$_POST['jsonData'];
$Data=json_decode($jsonData);
echo var_dump($Data);
echo '</pre>';
输出:我得到NULL
.
我不明白我做错了什么
最新更新:
我运行此测试:
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>';
输出
3
&
"
我可以确定链条已损坏,但是我不知道为什么,如何或如何修复它.
推荐答案
问题可能与以下用途有关:
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
需要解决此问题,才能从POST
和GET
need work around it to solve and use after read the data from POST
and GET
这篇关于发布Json String获取NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!