PHP / Angularjs /后的数据为空 [英] PHP/Angularjs/Post data empty
问题描述
我做选择两个字段(月份和原产地
)的形式提交给 AngularJS
控制器,我使用1.3.13版本的离子框架包装。
看着一个的console.log
在然后
法值正确填充。
将返回 q.promisse
至极有这个值: [目标,对象]
在 HTML
模板列表不填充了分辩期望值。
该值不填充 PHP API中的
。 PHP POST
变量
我怎么能填充 POST
数据?
在我的模板我也提交给搜索
方法:
<形式方法=邮报NG-控制器=AcpSearchCtrlNG提交=搜索(数据)>
<选择名称=月NG-模式=data.month>
<期权价值=01> 1< /选项>
在我的控制器o不要使用 http.post
和 promisse
:
.controller('AcpSearchCtrl',函数($范围,ApiAcpSearch,$ ionicLoading,$超时,$ HTTP,ApiAcpEndpoint,$ Q){
$ scope.search =功能(数据){
$ ionicLoading.show({
无背景:假的,
模板:'&所述p为H.;搜索...&下; / P>'
});
变种Q = $ q.defer();
$ scope.formData = {};
$ scope.submission = FALSE;
VAR参数=功能(数据){
VAR returnString ='';
对于(数据D){
如果(data.hasOwnProperty(d))的
returnString + = D +'='+数据并[d] +'和;';
}
返回returnString.slice(0,returnString.length - 1);
};
的console.log('FORMDATA:'+ $ scope.formData);
返回$ HTTP({
网址:ApiAcpEndpoint.url,
数据:参数($ scope.formData)
方法:POST,
标题:{内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8'}
})
。然后(功能(数据){
q.resolve(数据);
VAR ACP = {};
acp.qdata = [数据];
$ scope.data = acp.qdata;
$ ionicLoading.hide();
返回q.promise;
});
}
})
AngularJS,默认情况下,JSON格式发送数据。你不会找到它在普通的PHP全局变量($ _REQUEST,$ _ POST或$ _GET)。
您有两种方法来解决这个问题:
设置默认的内容类型的全球范围内为AngularJS(只设置前,请求将无法工作的头)。
VAR应用= angular.module(应用程序,[]);
的app.config(['$ httpProvider',函数($ httpProvider){
$ httpProvider.defaults.headers.post [的Content-Type] =应用/的X WWW的形式urlen codeD;
$ httpProvider.defaults.headers.common [X-要求,以] =XMLHtt prequest
}]);
另一种方法是,你处理的方式AngularJS发送在PHP中的数据:
$ angularJSData = json_de code(的file_get_contents(PHP://输入));//所以如果你数组形式需要json_de code将创建一个对象
$ angularJSData =(阵列)$ angularJSData
通过这些知识,你可以创建一个功能,甚至是你自己的全球性的。
I do choose two fields (month and origin
) in a form and submit it to an AngularJS
controller, i am using 1.3.13 version packaged with Ionic framework.
Watching a console.log
inside then
method the values are populated correctly.
The q.promisse
wich is returned have this value: [object, object]
.
The list of the HTML
template is not populated with the rigth expected values.
The values does not populate the PHP POST
variable in the PHP API
.
How can i populate the POST
data ???
In my template i do submit to search
method :
<form method="post" ng-controller="AcpSearchCtrl" ng-submit="search(data)">
<select name="month" ng-model="data.month">
<option value="01">January</option>
And in my controller o do use http.post
and a promisse
:
.controller('AcpSearchCtrl', function($scope, ApiAcpSearch, $ionicLoading, $timeout, $http, ApiAcpEndpoint, $q) {
$scope.search = function(data) {
$ionicLoading.show({
noBackdrop: false,
template: '<p>searching ...</p>'
});
var q = $q.defer();
$scope.formData = {};
$scope.submission = false;
var param = function(data) {
var returnString = '';
for (d in data){
if (data.hasOwnProperty(d))
returnString += d + '=' + data[d] + '&';
}
return returnString.slice( 0, returnString.length - 1 );
};
console.log('formData : '+$scope.formData);
return $http({
url:ApiAcpEndpoint.url,
data : param($scope.formData),
method : 'POST',
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
})
.then(function(data) {
q.resolve(data);
var acp = {};
acp.qdata = [ data ];
$scope.data = acp.qdata;
$ionicLoading.hide();
return q.promise;
});
}
})
AngularJS, by default, sends data in the JSON format. You won't find it in the regular PHP globals ($_REQUEST, $_POST or $_GET).
You have two ways to solve this issue:
Set the default Content-Type globally for AngularJS (just setting the header before the request will not work).
var app = angular.module("app", []);
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
}]);
The alternative is that you handle the way AngularJS sends the data in PHP:
$angularJSData = json_decode(file_get_contents("php://input"));
// json_decode will create an object so if you need in array format
$angularJSData = (array)$angularJSData
With this knowledge you can create a function or even your own global.
这篇关于PHP / Angularjs /后的数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!