无法通过$_get、$_POST&$_Angularjs中的REQUEST


Unable to get posted data by $_GET, $_POST & $_REQUEST in Angularjs

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

有人能告诉我为什么$_GET、$_POST和$_REQUEST不工作吗?

My 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!';
}

如有任何协助,我们将不胜感激。

为了配合我关于php.ini中可能存在错误设置的评论,这篇文章(死链接)(通过我的评论中的这个链接)还提供了以下报价:

如果内容类型为空或在HTTP消息中无法识别,则PHP$_POST数组为空。不确定这是一个错误还是设计…

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

  1. php.ini post_max_size设置中的语法错误
  2. 从前端发送的值错误或内容类型标头为空

如果你对post_max_size的设置是正确的,那么可能是angular正在删除帖子的Content-Type标题。根据这个问题和这个问题,如果没有有效负载或任何请求都有空的有效负载,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})
    // ...