$ http.get应该如何工作,我总是从PHP中得到答案

$ http.get应该如何工作,我总是从PHP中得到答案

问题描述:

正如我在标题中写的,我无法从PHP获得任何正确答案。任何人有任何想法?

的Javascript

var app = angular.module("appMovies", []); 


app.controller("listMovies", ["$scope", "$http", function($scope, $http){ 

    getMovies($http); 

}]); 


function getMovies(_http){ 

    _http.get("movies.php", {data:{"getList":"LISTA"}}) 
     .success(function(data, status, header, config){ 
      console.log(data); 
     }) 
     .error(function(data, status, header, config){ 
      //console.log(data, status, header, config); 
     }); 

} 

PHP

var_dump(file_get_contents("php://input")); 

于是,我知道了......对不起,我的坏。显然,$ _GET只能从URL获取数据,所以我应该写

$http.get("movie.php/?getList=LISTA")... 
+0

为什么你使用'file_get_contents'一个GET请求? – charlietfl 2014-09-21 23:43:40

+0

是的,我在这里看到了另一个例子 – Donovant 2014-09-22 06:52:01

试试它的另一种方式:

$http.post("movies.php", {data: {"getList": "LISTA"}}). 
     success(function (_data, _status) { 
     }) 
     .error(function (_data, status) { 
     }); 

而在你的PHP代码,你可以使用则:

$postData = file_get_contents("php://input"); 
$request = json_decode($postData); 
$request->_data; 
+0

为什么要用.post代替.get on javascript? – Donovant 2014-09-22 09:05:33

+0

我在代码中使用了此功能,并具有file_get_contents功能。我在GET方面遇到了很多麻烦,所以我使用了我的发布代码 – graphefruit 2014-09-22 10:47:40

+0

因此,$ http.get仅用于获取没有任何参数的数据? – Donovant 2014-09-22 12:32:29

它看起来像混合了GET和POST请求。要在Angular/PHP中使用GET,您需要在服务器上使用params(查询字符串参数)而不是data(用于POST正文),而使用_$GET而不是file_get_contents("php://input")(它给出POST正文)。

所以在浏览器中,像

_http.get("movies.php", {params: {"getList":"LISTA"}}) 

和服务器

var_dump($_GET); 
+0

我做了,但是它说undefined – Donovant 2014-09-22 09:06:19

+0

“params”是Angular还是PHP的特殊词? – Donovant 2014-09-22 09:10:36

+0

“params”是'$ http'的选项,详见https://docs.angularjs.org/api/ng/service/$http – 2014-09-23 20:22:40