$ 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")...
试试它的另一种方式:
$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;
为什么要用.post代替.get on javascript? – Donovant 2014-09-22 09:05:33
我在代码中使用了此功能,并具有file_get_contents功能。我在GET方面遇到了很多麻烦,所以我使用了我的发布代码 – graphefruit 2014-09-22 10:47:40
因此,$ 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);
为什么你使用'file_get_contents'一个GET请求? – charlietfl 2014-09-21 23:43:40
是的,我在这里看到了另一个例子 – Donovant 2014-09-22 06:52:01