AngularJS HTTP发布到PHP [英] AngularJS HTTP Post to PHP
问题描述
我正在使用$http
方法将某些数据从angularjs
控制器发布到php
页面.
I am posting some data to a php
page from angularjs
controller using the $http
method as shown below.
$scope.login = function(){
var data = {
'username' : $scope.username,
'password' : $scope.password
};
$http.post('login.php', data).success(function(response){
console.log(response);
});
};
我知道我可以使用<
$postdata = json_decode(file_get_contents('php://input'), true);
但是我想知道angularjs
中是否有更好的方法,因此我可以在php
中使用简单的$_POST
来检索数据.
But I was wondering if there is better way in angularjs
so I could use the simple $_POST
in php
to retrieve the data.
在jQuery
中,我们可以简单地使用$.post
,而这些在$_POST
中可用于php
.为什么angularjs
并不是那么简单.
在angularjs
中执行$http
post
请求时,有什么方法可以填充php
的$_POST
.请帮忙.
In jQuery
we could just simply use $.post
and those are available for php
in $_POST
. Why for angularjs
that is not that simple.
Is there any way to populate $_POST
of php
when we do a $http
post
request in angularjs
. Please help.
推荐答案
您需要将请求转换为php known
格式.
我使用jQuery
$.param
方法进行构建.您可以编写自己的.
You need to convert your request to php known
format.
I use jQuery
$.param
method to build it. You can write your own.
app.config(['$httpProvider', function($http) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http.defaults.transformRequest = function(data) {
return $.param(data);
};
}]);
urlencoded
遵循简单格式:
urlencoded
is followed simple format:
username=Name&password=My%20Password
这篇关于AngularJS HTTP发布到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!