在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?
答案 0 :(得分:1)
使用对象作为参数而不是字符串:
params: {index:1},
然后在your.php中
print_r($_GET['index']);
答案 1 :(得分:0)
你可以试试这个
$_GET['index']
当params
为?
时,{p> Method
与GET
后的网址相同