【问题标题】:How do I retrieve parameters from an AngularJS GET HTTP request inside php?如何从 php 中的 AngularJS GET HTTP 请求中检索参数?
【发布时间】:2014-09-20 03:03:05
【问题描述】:

在 Angular 方面,我正在尝试执行如下所示的 GET http 请求:

$scope.getQuestion = function() {
    //$http.post($scope.url, { src : $scope.question});

    var request = $http({
        method: "get",
        url: $scope.url,
        params:  'index =1',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });

    /* Check whether the HTTP Request is Successfull or not. */
    request.success(function (data) {
        console.log("GET literally worked");

        $scope.loginMessage = data;//angular.fromJson(data);
        //console.log($scope.testArray["question2"]);

但我不确定如何在 php.ini 中从中检索参数(索引 = 1)。我尝试将其作为 JSON 进行,但它似乎不起作用?

PHP端:

<?php


$data = file_get_contents("php://input");
$src = json_decode($data);
//var_dump($_POST);
//$src     = $_POST['src'];
//@$toOut = $src->question;
//file_put_contents("output", $toOut);

/*$arr = array (
    0 => "does this work?",
    "question2" => "i mean i guess?"

    );
$jsonString = json_encode($arr);

echo $jsonString;*/

$numArr = $src -> params;
echo $numArr -> index;
?>

所以我对如何读取参数感到困惑,因为我们没有将它作为 $data 传递给 php?

【问题讨论】:

    标签: php json angularjs get


    【解决方案1】:

    使用 object 作为参数而不是字符串:

    params:  {index:1},
    

    然后在你的.php中

    print_r($_GET['index']);
    

    【讨论】:

      【解决方案2】:

      你可以试试这个

      $_GET['index']
      

      MethodGET 时,params? 之后的url 相同

      【讨论】:

      • 我尝试这样做,它说索引未定义
      猜你喜欢
      • 2012-09-16
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 2020-02-27
      相关资源
      最近更新 更多