这是我的视图和控制器,我将值发布到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?
答案 0 :(得分:2)
使用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');
})
}