doufa5001 2014-12-20 19:35
浏览 55
已采纳

$ _POST var未填充而$ HTTP_RAW_POST_DATA获取数据

i use this function in javascript to communicate with the server php script:

    ajax = function( url, ready, json=null, method = 'post') {
    var response, request = xhr();
    request.open( method, url, true );
    request.onreadystatechange = function() {
        if( request.readyState == 4 ) {
            if( typeof ready == 'function' ){
                return ready( request );
            } else {    return JSON.parse( request.responseText );}
        } 
    }
    if(json !== null){
        request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        return request.send( JSON.stringify( json ) );
    }else { return request.send( null );}
}

i made the XMLHttpRequest after i open the connection using post method. i also set the setRequestHeader to parse JSON: ("Content-Type", "application/json;charset=UTF-8"); and i used this function to colect data from html form:

formToValues = function (id) {
    var i, values = {}, form = document.getElementById(id);
    //var fields = form.querySelectorAll('input,textarea,select'),
    for (i = 0; i < form.length ;i++) {
        if( form.elements[i].name !== "" ){
            values[form.elements[i].name] = form.elements[i].value;
        }
    }
    return values;
}


ajax('a.php', function(response){
    console.log(response);
}, formToValues("regForm") );

here is also my html form:

<form id="regForm" action="javascript:;" method="post" />
<p>
<label for="name">Name</label>
<input type="text" name="name" value="xhr" />

<label for="email">Email:</label>
<input type="email" name="email" value="ro@ew.gq" />

<label for="password">Password:</label>
<input type="password" name="password" value="pass" />

<input type="button" value="Search" />
</p>

now i have a liitle problem reciving data in php script: a.php:

<?php
var_dump(  $GLOBALS );
$ar= array( "a"=>2,"b"=>3, "c"=>json_decode( $_REQUEST['params'] ));
echo json_encode($ar);
?>

seems like php take my data sended from javascrip as string and store it in HTTP_RAW_POST_DATA

["HTTP_RAW_POST_DATA"]=> string(51) "{"name":"xhr","email":"ro@ew.gq","password":"pass"}"

    "array(7) {
  ["GLOBALS"]=>
  array(7) {
    ["GLOBALS"]=>
    *RECURSION*
    ["HTTP_RAW_POST_DATA"]=>
    string(51) "{"name":"xhr","email":"ro@ew.gq","password":"pass"}"
    ["_POST"]=>
    array(0) {
    }
    ["_GET"]=>
    array(0) {
    }
    ["_COOKIE"]=>
    array(0) {
    }
    ["_FILES"]=>
    array(0) {
    }
    ["_REQUEST"]=>
    array(0) {
    }
  }

now i can get data with php://input will not be a problem but i am concernet about security and it dos'n sems ok, and it's one more thing to get in consideration : starting with php version 5.6.0. $HTTP_RAW_POST_DATA — Raw POST data is deprecated~ so what should i do? Tkanks

  • 写回答

1条回答 默认 最新

  • duanjia6959 2014-12-20 19:39
    关注

    PHP does not populate the $_POST superglobal when the request content type is application/json (only application/x-www-form-urlencoded and multipart/form-data).

    The suggestion is to use json_decode() and php://input.

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

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题