Ade*_*rsh 4 php jquery post angularjs
我使用如下所示的方法php从angularjs控制器向页面发布一些数据$http.
$scope.login = function(){
var data = {
'username' : $scope.username,
'password' : $scope.password
};
$http.post('login.php', data).success(function(response){
console.log(response);
});
};
Run Code Online (Sandbox Code Playgroud)
我知道我可以php使用以下方法检索此数据:
$postdata = json_decode(file_get_contents('php://input'), true);
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的方法,angularjs所以我可以使用simple $_POSTin php来检索数据.
在jQuery我们可能只是简单地使用$.post那些可用于php在$_POST.为什么angularjs这不那么简单.有没有什么办法来填充$_POST的php,当我们做$http post的请求angularjs.请帮忙.
您需要将您的请求转换为php known格式.
我用jQuery $.param方法来构建它.你可以自己写.
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);
};
}]);
Run Code Online (Sandbox Code Playgroud)
urlencoded 遵循简单的格式:
username=Name&password=My%20Password
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4448 次 |
| 最近记录: |