为什么我的角度POST请求一个PHP页面无法建立POST数组? [英] Why is my Angular POST request to a PHP page not setting up the POST array?

查看:256
本文介绍了为什么我的角度POST请求一个PHP页面无法建立POST数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角后发送到我的PHP文件,但在PHP文件,我无法访问从岗位变任何东西。它返回我的成功串,但没有在这之后,所以我对后回报的成功 - - - - - 。其中数据应该是破折号之间

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对象:

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

角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页面试图找到张贴的值:

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

从GOOGLE开发者工具的请求的图片:

Picture of the Request from GOOGLE developer tools:

从请求返回数据的图片(见PHP code表示,成功是来自):

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

为什么我不能访问后变量?我失去了一些东西在这里?

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

推荐答案

有关PHP的解码,我用下面的code,这给了我直接通入POST数据,这在我的情况是JSON。显然,JSON是不支持的数据结构,使PHP无法自动解析到POST数组:

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

说明:
1线获取被从请求(发送过来在这种情况下,它是从角控制器张贴在OP的原始数据。
2号线德codeS JSON数组在第一步中抓起,形成正确稍后解析。注意,在这种情况下,步骤一和二都返回完全相同的寻找对象。
3号线采用格式化的JSON数据和德codeS它变成一个PHP数组。现在,您可以使用以下方法来访问数组的部分:

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!

这篇关于为什么我的角度POST请求一个PHP页面无法建立POST数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆