<!DOCTYPE html>
<html>
<head>
<title>login</title>
<script src="angular.min.js"></script>
<style type="text/css">
#a{
display: none;
}
</style>
</head>
<body>
<div ng-app="myapp" ng-controller="myctrl">
<p>Input something in the input box:</p>
<input type="text" ng-model="username" placeholder="username"><br>
<input type="password" name="password" ng-model="password" placeholder="password"><br>
<button ng-click="login()">login</button>
<p id="a">
{{ resmsg }}<br>
<br></p>
</div>
<script type="text/javascript">
var app= angular.module('myapp',[]);
app.controller('myctrl', function ($scope , $http, $templateCache) {
$scope.resmsg ="";
$scope.login = function()
{
$http({
method: 'POST',
url:'http://localhost/ng/myphp.php',
data:{username : $scope.username, password : $scope.password},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
cache: $templateCache
}).success(function(response) {
$scope.resmsg = response;
}).error(function(response){
})
$scope.resmsg = response;
}
})
</script>
</body>
</html>`
myphp.php
<? php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$username = $request->username;
$password = $request->password;
if($username == "admin" && $password== "admin"){
echo "1";
}
else {
echo "0";
}
?>
I was trying to create a simple login form using angularjs, php i have tried lot to send my form data to php file using http.post() method
in angular js but not getting any thing as a response and getting errormsg (screenshot) , the error is like there is no function like success help me to get rid from this error
最佳答案
您使用的 Angular 版本是什么? $http.success
在 Angular 1.5 中已弃用,并在 Angular 1.6 中删除。您应该使用$http.then
,像这样:
$http({
method: 'POST',
url:'http://localhost/ng/myphp.php',
data:{username : $scope.username, password : $scope.password},
cache: $templateCache
}).then(function(response) {
$scope.resmsg = response.data;
}).catch(function(response){
var err = response.data;
// process error
})
接下来,在打开的 php 标签中出现错误,有一个额外的空格(使用 <?php
而不是 <? php
。
此外,您的服务器似乎没有使用 PHP 处理该文件,它只是返回 PHP 代码本身。尝试直接访问脚本并查看它是否返回代码或执行它。如果它以字符串形式返回代码,则表明您的 Web 服务器配置存在错误。
关于javascript - angularjs http.post() 未收到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573168/