AngularJS通过邮寄发送数据时检索数据问题

瓦希德·纳杰菲(Vahid Najafi)

我将从网址获取的ID发送到服务器(PHP页面)。

我从url获取并通过postangularjs中方法发送的方式:

注意:我检索数据,并在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;

我得到的错误是:

错误

达伦·温赖特(Darren Wainwright)

该错误基本上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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从ckeditor textarea提交ajax表单提交值后未通过邮寄发送

来自分类Dev

通过邮寄表格序列化发送数据

来自分类Dev

检索通过ajax发送的数据

来自分类Dev

从数据库中检索数据并通过邮件发送

来自分类Dev

Laravel 5:检索通过Ajax发送的POST数据

来自分类Dev

Laravel 5:检索通过Ajax发送的POST数据

来自分类Dev

数据检索问题

来自分类Dev

检索数据时出错

来自分类Dev

尝试从Firebase检索数据时遇到问题

来自分类Dev

检索json数据时遇到问题。如何找回

来自分类Dev

尝试从本地存储中检索数据时出现问题

来自分类Dev

JS:单击事件时从动态表检索数据的问题

来自分类Dev

从json检索嵌套数据时出现问题

来自分类Dev

从内部观察中检索数据时遇到问题

来自分类Dev

通过AJAX发送数据

来自分类Dev

通过组件发送数据

来自分类Dev

通过连接发送数据

来自分类Dev

通过AngularJS发送带有多个数据的HTTP Post

来自分类Dev

从邮寄表格中获取数据

来自分类Dev

使用QSerialPort发送数据时出现问题

来自分类Dev

Android / Kotlin:使用URLConnection发送数据时出现问题

来自分类Dev

套接字编程问题,退出时发送数据

来自分类Dev

Firebase通过密钥检索数据

来自分类Dev

从数据库中检索数据的问题

来自分类Dev

通过SOAP发送xml数据时无法获得响应

来自分类Dev

通过 Socket TCP/IP 发送数据时出错

来自分类Dev

通过 MFMailcomposeController 发送时在 sqlite 中发现空数据

来自分类Dev

无法通过检索“数字”从 Firebase 检索数据

来自分类Dev

将数据从Angularjs发送到php时出错