我正在編寫一個簡單的登錄服務,我只是試圖製作用戶名和密碼,將它們發送到php並獲取簡單的console.log'GJ'。一切工作正常,直到我從我的PHP函數得到響應「錯誤」。我嘗試了console.log我發送的數據,它說未定義。我嘗試了與JSON一樣的東西。任何人都可以查看我哪裏有錯AngularJS表單返回undefined
HTML
<div class="container login">
<div class="column column-4">
<form name="from1" role="form">
<label for="">{{user.mail}}</label> <br>
<input type="text" placeholder="username" required ng-
model="user.mail"><br>
<label for="">{{user.pass}}</label> <br>
<input type="text" placeholder="password" required ng-
model="user.pass"><br>
<button name="submit" class="btn btn-default" ng-click="login()">
Submit</button>
</form>
</div>
</div>
MAIN.JS
app.factory('loginService',function($http){
return{
login:function(user,scope){
var $promise = $http.post('http://site1.local/user.php', user);
//send data to user.php
$promise.then(function(msg){
if(msg.data=='success') console.log("GJ");
else console.log("DARN IT");
});
}
}
});
app.controller('LoginCtrl', function($scope, loginService) {
$scope.login=function(user){
console.log(user);
loginService.login(user,$scope); //call login service
};
});
PHP
<?php
$data = file_get_contents("php://input");
$user = json_decode($data);
if($user->mail=='admin' && $user->pass=='1234')
print 'success';
else
print 'error';
?>
如果您檢查在$ promise.then功能 '味精',你看到了什麼? – Marcidius
我consle.log(味精),我得到 undefined –