duanqiao1926 2017-01-18 11:10
浏览 119
已采纳

在客观的ajax请求之后,filter_input与$ _POST上的直接访问之间的区别

I have different results by using filter_input(INPUT_POST, 'attribute') and $_POST['attribute'] and don't know why this happens.

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)
  }
});

My PHP-Script looks like that:

// type php
$requestMethod = INPUT_POST;
$response = [
  "fi-result" => filter_input($requestMethod, 'formData'),
  "direct-result" => $_POST['formData'];
];
echo json_encode($response);

the result what is coming back is not what i was awaiting because the access via filter_input returns falsein 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": ""
  }
}

Why are there differences between using filter_input and direct access on $_POST?

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?

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.

  • 写回答

1条回答 默认 最新

  • douzhong6480 2017-02-19 17:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化