![](/img/trans.png)
[英]$http.post() angularjs response function not executing in right order
[英]angularjs http.post() not receiving response
<!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";
}
?>
我試圖使用angularjs創建一個簡單的登錄表單,php我嘗試了很多嘗試使用http.post()方法將表單數據發送到php文件
在Angular js中,但沒有得到任何響應並得到errormsg(截圖),錯誤就像沒有像成功這樣的功能可以幫助我擺脫這個錯誤
您使用的是哪個版本的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服務器配置中存在錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.