我将从网址获取的ID发送到服务器(PHP页面)。
我从url获取并通过post
angularjs中的方法发送的方式:
(注意:我检索数据,并在console.log()中打印正确,问题是我要将它们放到$ scope中)
.controller('PlaylistCtrl', function($scope, $stateParams, $http) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http({
method: 'POST',
url: 'http://cms.focusweb.ir/Json/get_article',
data: { id: $stateParams.playlistId },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(response) {
// This works right
console.log(response);
// PROBLEM IS HERE
angular.forEach(response, function(response){
$scope.articles_content.push(response);
});
})
.error(function(data, status, headers, config) {
// handle error things
});
});
我的PHP代码是:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$article_ID = $request->id;
我得到的错误是:
该错误基本上articles_content
是说不存在(undefined
)。因此'push' of undefined
您需要先定义数组,然后再尝试使用它。
$scope.articles_content = []
因此应为:
.controller('PlaylistCtrl', function($scope, $stateParams, $http) {
$scope.articles_content = []; // Add this in here
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http({
method: 'POST',
url: 'http://cms.focusweb.ir/Json/get_article',
data: { id: $stateParams.playlistId },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(response) {
// This works right
console.log(response);
angular.forEach(response, function(response){
// because you now defined articles_content
// as an array, the push() will now work :)
$scope.articles_content.push(response);
});
})
.error(function(data, status, headers, config) {
// handle error things
});
});
在$scope
已定义,这就是为什么你可以动态地添加任何东西给它。除此之外,您需要首先定义您的结构。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句