【问题标题】:How to get the values from php to angular js?如何将值从 php 获取到 angular js?
【发布时间】:2017-06-30 10:35:16
【问题描述】:

这是我的视图和控制器,我将值发布到 php 和 mysql。然后我从数据库中获取/检索值?我没有?

<html>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>

<body>
    <div ng-app="samp">

        <div ng-controller="formController" >
            <form ng-submit="submity()">
                <input type="text" ng-model="test.namee">
                <input type="text" ng-model="test.email">
                <input type="submit" value="okay">
            </form>
            ==>{{list}}hyhygtgty
        </div>

    </div>

</body>
</html>
<script type="text/javascript">
var app=angular.module('samp',[]);
app.controller('formController',function($scope,$http)
    {
        $scope.test={};

        $scope.submity=function()
        {
            console.log($scope.test);
            $http(
            {
                method:"POST",
                url:"sample3.php",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                data:{'name':$scope.test.namee,'name1':$scope.test.email},
                success:function(data)
                {
                 alert('sss')
                }
            });
        }

        $scope.myctrl = function(){
            $http(
            {
                method:"GET",
                url:"sample4.php",
                //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                //data:{'name':$scope.test.namee,'name1':$scope.test.email},
                success:function(data)
                {
                    $scope.list=data;
                    console.log(data);

                }
            });

        }
        $scope.myctrl()

    });
</script>

这是我的 php 代码

<?php
$conn=mysqli_connect('localhost','root','','boot');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $query=mysqli_query($conn,"select * from boots");
    $data=array();
    while($row = mysqli_fetch_array($query))
    {
        $data[]=$row;
    }

    print json_encode($data);
?>

如何将响应数据核心 php 发布到 angular?

【问题讨论】:

    标签: javascript php mysql angularjs


    【解决方案1】:

    像这样使用then 而不是success 来捕获http 响应。而success 不是http 属性的一部分。删除它并使用then 来捕捉这样的承诺

    $scope.submity = function() {
        $http({
            method: "POST",
            url: "sample3.php",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: {
                'name': $scope.test.namee,
                'name1': $scope.test.email
            },
        }).then(function(response) {
            console.log(response.data);
            alert('sss');
        })
    }
    

    【讨论】:

    • 我认为您可以发表评论而不是回答。 :(
    • @Ramesh Rajendran 我发布了一个答案,因为他将成功作为 http req 的属性。不兑现承诺
    • 那么您应该在回答中提到这一点。对吗?
    猜你喜欢
    • 1970-01-01
    • 2012-07-16
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    相关资源
    最近更新 更多