2014-09-20 103 views
1

在角邊,我試圖做類似下面的GET HTTP請求:如何從PHP中的AngularJS 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"]); 

但我不知道如何從它PARAMS(指數= 1)在PHP。我試圖做它作爲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因爲我們沒有把它傳遞給PHP作爲$數據混淆?

回答

0

你可以試試這個

$_GET['index'] 

params的工作方式相同網址?後,當MethodGET

+0

我試圖做到這一點,它說索引是undefined – Skorpius 2014-09-20 03:21:25

1

使用對象作爲PARAM而不是字符串:

params: {index:1}, 

然後在你的.php

print_r($_GET['index']);