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条)

报告相同问题?

悬赏问题

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