客观ajax请求后,filter_input和$ _POST上的直接访问权限之间的差异 [英] Difference between filter_input and direct acces on $_POST after objective ajax request
问题描述
使用filter_input(INPUT_POST, 'attribute')
和$_POST['attribute']
会有不同的结果,并且不知道为什么会这样.
I have different results by using filter_input(INPUT_POST, 'attribute')
and $_POST['attribute']
and don't know why this happens.
Post-Request由具有JQuery的JavaScript构建发送,如下所示:
The Post-Request is send by a JavaScript build with JQuery and looks like that:
// type javaScript
var formData = {
field_a: "valueA",
field_b: "",
field_c: undefined
};
$.ajax({
url: 'serverAddress',
data: {action: 99, formData: formData},
dataType: 'json',
method: 'post',
success: function(){
console.log(arguments)
}
});
我的PHP脚本如下:
// type php
$requestMethod = INPUT_POST;
$response = [
"fi-result" => filter_input($requestMethod, 'formData'),
"direct-result" => $_POST['formData'];
];
echo json_encode($response);
返回的结果不是我在等待的结果,因为通过filter_input
进行的访问在我的测试中返回了false
,而不是像对超级全局$ _POST的直接访问之类的json对象.
the result what is coming back is not what i was awaiting because the access via filter_input
returns false
in my tests and not an json object like the direct access on the super global $_POST.
// type json response
{
"fi_result": false,
"direct-result": {
"field_a": "valueA",
"field_b": ""
}
}
为什么在$ _POST上使用filter_input和直接访问之间有区别?
Why are there differences between using filter_input and direct access on $_POST?
我不想访问超级全局$ _POST.是否可以使用上述的filter_input而不在JavaScript中将formData编码为String并在编码后的一个简单步骤中在PHP中对其进行解码?
I don't want to access the super global $_POST. Is there any way to use filter_input like above without encode formData to a String in JavaScript and decode it in PHP one simple step after encoding?
顺便说一句.我正在使用TypeScript生成我的JavaScript.那不支持FormData对象(编译器在new FormData()
上引发错误).所以我不能使用它.
By the way. I'm using TypeScript to generate my JavaScript. That is not supporting the FormData Object (transpiler throws error on new FormData()
). So i can't use this.
推荐答案
我在PHP文档的深处找到了答案. POST不是为传输深层对象而构建的. filter_input方法尝试获取简单的数据类型,例如字符串或整数.此方法不解析内部,因此我必须将其作为JSON字符串发送并解码,否则我无法使用filter_input.
I found the answer deep in the PHP docs. POST is not build to transport deep object. And filter_input method tries to get simple datatypes like string or int. this method does not parse internal so i have to send it as JSON string and decode it or i can't use filter_input in my case.
我先读了,现在发送了字符串.
i took the first and send now strings.
这篇关于客观ajax请求后,filter_input和$ _POST上的直接访问权限之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!