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 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印