如何从php中的AngularJS GET HTTP请求中检索参数?

时间:2014-09-20 03:03:05

标签: php json angularjs get

在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中检索params(index = 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;
?>

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

2 个答案:

答案 0 :(得分:1)

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

params:  {index:1},

然后在your.php中

print_r($_GET['index']);

答案 1 :(得分:0)

你可以试试这个

$_GET['index']
params?

时,{p> MethodGET后的网址相同