douwan2664 2014-07-07 02:10
浏览 41
已采纳

为什么我的Angular POST请求PHP页面没有设置POST数组?

I have a angular post that sends to my php file, but in the PHP file, I cannot access anything from the post variable. It returns my SUCCESS string, but nothing after that, so my return on the post is "SUCCESS - - - - - " where the data should be between the dashes.

JSON/JS object:

DESCRIPTION: "123321"
LOCATION: "ab_calgary_eighth_ave_pl"
NAME: "123321"
QUANTITY: 123321
TYPE: "cycle"

Angular POST Code:

        $scope.insertNewInventoryItem = function()
        {
            if(typeof ($scope.newItem.LOCATION) == 'undefined')
                alert("LocationCannot Be Empty.  Please Select An Option From The Drop Down.");

            else if(typeof ($scope.newItem.TYPE) == 'undefined')
                alert("Type Cannot Be Empty.  Please Select An Option From The Drop Down.");

            else
            {
                $http.post("dataaccess/addnewinventory.php", $scope.newItem).then(onAddNewComplete, onAddNewError);
            }
        }

PHP Page attempting to find the posted values:

<?php
$con = mysqli_connect("localhost", "dbadminuser", "password", "database_demo_inventory");

// Check connection
if (mysqli_connect_errno())
{
    echo "FAIL - Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    echo "SUCCESS - " . $HTTP_POST_VARS['newItem.NAME'] . " - " . $HTTP_POST_VARS['TYPE'] . " - " . $HTTP_POST_VARS["QUANTITY"] . " - " . $HTTP_POST_VARS . " - " . $_POST[0];
}

mysqli_close($con);
?>

Picture of the Request from GOOGLE developer tools: enter image description here

Picture of return data from the request (see PHP code for where SUCCESS is coming from): enter image description here

Why can I not access the post variables? Am I missing something here?

  • 写回答

4条回答 默认 最新

  • dousong2967 2014-07-09 03:26
    关注

    For decoding the PHP, I used the following code, which gave me direct access into the POST data, which in my case was JSON. Apparently JSON is a unsupported data structure, so PHP failed to parse it automatically into the POST array:

    $jsonText = file_get_contents('php://input');
    $decodedText = html_entity_decode($jsonText);
    $myArray = json_decode('[' . $decodedText . ']', true);
    

    Explanation: Line 1 gets the raw data that was sent over from the request (in this case it was from the Angular Controller posted in the OP. Line 2 decodes the JSON array grabbed in step one, correctly forming it to parse later. Note that in this case, steps one and two both return the exact same looking object. Line 3 takes the formatted JSON data and decodes it into a PHP array. Now you can use the following to access parts of the array:

    $myvar = $myArray[0]["SOME_VARIABLE"];
    

    Thanks for the answers guys!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?