【问题标题】:Unable to get posted data by $_GET, $_POST & $_REQUEST in AngularjsAngularjs 中无法通过 $_GET、$_POST 和 $_REQUEST 获取发布的数据
【发布时间】:2014-04-18 15:02:03
【问题描述】:

我是 AngularJs 的新手。我试图使用 angularjs 创建一个简单的应用程序。 在一个简单的应用程序中,我无法在我的 php 脚本中通过 $_POST、$_GET 或 $_REQUEST 打印发布的数据。 在使用file_get_contents("php://input")`时,我可以打印数据。

谁能告诉我为什么 $_GET、$_POST 和 $_REQUEST 不起作用?

我的 Index.html 代码:

<!DOCTYPE html>
<html ng-app>
<head>
<title>Search form with AngualrJS</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
    <script src="http://code.angularjs.org/angular-1.0.0.min.js"></script>
    <script src="search.js"></script>
</head>

<body>

    <div ng-controller="SearchCtrl">
    <form class="well form-search">
        <label>Search:</label>
        <input type="text" ng-model="test" class="" placeholder="Keywords...">
        <button type="submit" class="btn" ng-click="search()">Search</button>
        <p class="help-block">Try for example: "php" or "angularjs" or "asdfg"</p>      
    </form>
<pre ng-model="result">
{{result}}
</pre>
   </div>
</body>

</html>

search.js 代码:

function SearchCtrl($scope, $http) {
    $scope.url = 'search.php'; // The url of our search

    // The function that will be executed on button click (ng-click="search()")
    $scope.search = function() {

        // Create the http post request
        // the data holds the keywords
        // The request is a JSON request.
        $http.post($scope.url, { "data" : $scope.test}).
        success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
            $scope.result = data; // Show result from server in our <pre></pre> element
        })
        .
        error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;         
        });
    };
}

Search.php 代码:

<?php
// The request is a JSON request.
// We must read the input.
// $_POST or $_GET will not work!

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

$objData = json_decode($data);

// perform query or whatever you wish, sample:

/*
$query = 'SELECT * FROM
tbl_content
WHERE
title="'. $objData->data .'"';
*/

// Static array for this demo
$values = array('php', 'web', 'angularjs', 'js');

// Check if the keywords are in our array
if(in_array($objData->data, $values)) {
    echo 'I have found what you\'re looking for!';
}
else {
    echo 'Sorry, no match!';
}

我们将不胜感激。

【问题讨论】:

  • 如果没有示例 sn-p,我们将无法告诉您您正在尝试做什么,无论是在角度方面还是在 PHP 方面。
  • 你好观察者.. 我已经在我的问题中包含了代码。
  • 在 search.php $_POST,$_GET 中不起作用,为什么?
  • 我不知所措.. Search.php 中var_dump($_POST); 的结果是什么?
  • nothing..它打印空白数组。

标签: php angularjs


【解决方案1】:

为了配合我对 php.ini 中可能存在错误设置的评论,this article(dead link)(通过 this one 链接,来自我的评论)也提供了以下引用:

如果 Content-Type 为空或在 HTTP 消息中无法识别,则 PHP $_POST 数组为空。不确定这是错误还是设计使然……

症状相同,($_POST 为空,但可通过 php://input 获得数据),这导致出现此错误的可能原因有两个:

  1. php.ini post_max_size 设置中的语法错误
  2. 从前端发送的错误值或空 Content-Type 标头

如果您对 post_max_size 的设置正确,则可能是 Angular 正在删除帖子的 Content-Type 标头。根据this questionthis question 的说法,如果任何请求都没有负载或空负载,Angular 将删除 Content-Type 标头,我认为这是您的问题。

尝试将其添加到您的 SearchCtrl

function SearchCtrl($scope, $http) {
    $scope.url = 'search.php'; // The url of our search
    $scope.test = {test:'test'}; // Add me
    // ...

我怀疑您的 $_POST 数组将被填满,因为现在将与请求一起发送 Content-Type 标头,并且发送实际的请求数据。如果可行,则意味着您的 test 输入未正确绑定到范围,或者您正在提交空数据。尝试在 HTTP 请求之前添加一个保护子句:

if($scope.test) {
    $http.post($scope.url, { "data" : $scope.test})
    // ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多