使用表单而不是有角HTTP客户端

时间:2019-05-15 08:33:47

标签: php angularjs

我正在使用角度http客户端与数据库进行交互,并且一切正常,但是当我尝试使用表单将数据发布到同一链接时,我得到的数据是不确定的。

我正在尝试对值进行编码和解码,因为我知道在发出任何POST请求和发送数据之前,我需要执行angular.toJSON方法,但这没有用。

这是我的index.php,我从该表单收到POST请求。

if (empty($action)) {
    if ((($_SERVER['REQUEST_METHOD'] == 'POST')) && 
             (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false)) {

        $input = json_decode(file_get_contents('php://input'), true);

        $action = isset($input['action']) ? $input['action'] : null;
        $subject = isset($input['subject']) ? $input['subject'] : null;
        $data = isset($input['data']) ? $input['data'] : null;
    }

case 'createNote':
        // if I die() here, it prints the die()
        if(!empty($data)) {
            // if I die() here, $data is undefined.
            $data = json_decode($data);

            $user = $data[0];
            $comment = $data[1];
            $film_id = $data[2];
            $lastupdated = date("Y-m-d H:i:s");


            $sql = "INSERT INTO nfc_note (user, film_id, comment, lastupdated) 
                    VALUES (:user, :film_id, :comment, :lastupdated)";
        }
        break;

我用于发送POST请求的表单

<form action="index.php" method="POST">
    <input type="hidden" name="action" value="create">
    <input type="hidden" name="subject" value="note">
    <input type="hidden" name="data" value="<?php echo "['username','content', 1]"; ?>">

    <input type="submit" value="Submit">
</form>

如上所述,当我使用angular的http并传递如下参数时,它可以工作:

this.createNote = function (data) {

    var defer = $q.defer(),
    data = {
        action: "create",
        subject: "note",
        data: angular.toJson(data)
    };
    $http
        .post(urlBase, data)
        .success(function (response) {
            defer.resolve({
                data: response.data
            });
        })
        .error(function (error) {
            defer.reject(error);
        });

    return defer.promise;
};

使用表格时不起作用。我没有发现任何建议或错误?

2 个答案:

答案 0 :(得分:2)

您的PHP代码期望使用Json格式的数据,但没有得到。这是因为HTML表单以application/x-www-form-urlencoded的形式发送POST数据。

要同时支持两种数据格式,您需要在PHP代码上构建逻辑以检测这两种格式。 HTTP标头中提到了数据格式,您可以在其中进行检查。寻找Content-Type。对于用于HTML表单的POST数据,它是application/x-www-form-urlencoded,对于Json,它应该是application/json

您可以使用$_POST[<form_parameter>]在PHP中读取表单值。就您而言,$_POST['data']。为了简化HTML表单,您还可以将data数组拆分为表单中自己的输入。

有关更多信息,请参见此:https://www.smtpeter.com/en/documentation/json-vs-post

答案 1 :(得分:0)

我遇到了类似的问题 “使用表单而不是有角HTTP客户端时,对PHP的POST请求不会检索任何数据”。 通常使用流php来检测从Angular到PHP的数据

$data = file_get_contents('php://input');

但是当使用强化扫描代码时,它检测到xss漏洞,我必须更改方法。

通过这种方式解决了这个问题,希望对您有所帮助。 :-)

英语:

$http({
method: 'POST',
url: '<?php echo base_url(); ?>admin/saveTemplateEmail',
headers: {'Content-Type': 'application/json'},
data: {templateEmail:$scope.selectedTipology}}).then(function (response) {.....}

PHP:

$data = file_get_contents('php://input');
$post_val = json_decode($data, true);

我将其更改为以下

英语:

$http({
method: 'POST',
url: '<?php echo base_url(); ?>admin/saveTemplateEmail',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
data: jQuery.param({templateEmail:$scope.selectedTipology})}).then(function (response) {.....}

PHP:

$post_val = $_POST;