gpt4 book ai didi

javascript - 在 AngularJS 中使用 json 数据时出现身份验证问题

转载 作者:行者123 更新时间:2023-11-30 21:05:09 25 4
gpt4 key购买 nike

我是 Angularjs 的新手,在解析 JSON 响应时遇到问题。我已经创建了登录页面,为了访问数据,我使用了 JSON 文件。调用工厂函数 时出现错误。

HTML:

<form ng-submit="loginform(logcred)" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>
<div>
<label form="emailinput"><b>Email</b></label>
<input type="email" class="form-control" name="uname" id="emailinput" placeholder="you@example.com" ng-model="logcred.username" >
</div>

<div>
<label form="pwdinput"><b>Password</b></label>
<input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="logcred.password">
</div>

<div>
<button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="submit()">Login</button>
</div>
<br/>
</form>

AngularJS:

app.factory('auth',function($http,session) {
var authService = {};

authService.login = function(userCred) {
$http.post('http://localhost:3000/loginfo',userCred)
.then(
function successCallback(response){
console.log(response);
session.create(response.data.id);
var user = response.data;
if (user [user.username && user.password]) {
var userCred = user[user.username && user.password];
if (userCred.username == user.username && userCred.password == user.password) {
$scope.signinfo = response.data;
} else {
console.log('Error');
}
}
});
};

authService.isAuthenticated = function() {
return {
isAuthenticated : false,
user : null
}
};

return authService;
});

var app = angular.module('logpp', []);
app.controller('credientials', function($scope, $http,auth) {
$scope.logForm = {};

$scope.userCred = {
username : '',
password : ''
};

/*-----Form Submition-----*/

$scope.log = function(){
auth.isAuthenticated = true;
if (!$scope.logForm.$invalid) {
$scope.login($scope.userCred);
} else {
console.log('error');
return;
}
};

/*-----Calling Factory Function-----*/

$scope.login = function(userCred) {
auth.isAuthenticated = true;
auth.login(function(user) {
console.log('success');
},function(error) {
console.log("error");
});
}

当我输入我的用户名和密码并单击 login()btn 时,我的控制台中出现了 Possibly unhandled rejection,我不知道为什么。我重新检查了我的代码,当我提交表单时,错误是由 factory function 生成的,我在控制台中检查了它。但是我不明白我在做什么错误?

如有任何帮助/建议,我们将不胜感激。

最佳答案

兄弟,你已经将主应用程序变量声明为 myApp,并将工厂附加到应用程序变量。

var myApp = angular.module('myApp', []);


app.factory('auth',function($http,session) {
var authService = {};

更新的答案

var myApp = angular.module('myApp', []);


myApp.factory('auth',function($http,session) {
var authService = {};

authService.login = function(userCred) {
$http.post('http://localhost:3000/loginfo',userCred)
.then(
function successCallback(response){
console.log(response);
session.create(response.data.id);
var user = response.data;
if (user [user.username && user.password]) {
var userCred = user[user.username && user.password];
if (userCred.username == user.username && userCred.password == user.password) {
$scope.signinfo = response.data;
} else {
console.log('Error');
}
}
});
};

authService.isAuthenticated = function() {
return {
isAuthenticated : false,
user : null
}
};

return authService;
});


myApp.controller('credientials', function ($scope, $http, auth) {
$scope.logForm = {};

$scope.userCred = {
username: '',
password: ''
};

/*-----Form Submition-----*/

$scope.log = function () {
auth.isAuthenticated = true;
if (!$scope.logForm.$invalid) {
$scope.login($scope.userCred);
} else {
console.log('error');
return;
}
};

/*-----Calling Factory Function-----*/

$scope.login = function (userCred) {
auth.isAuthenticated = true;
auth.login(function (user) {
console.log('success');
}, function (error) {
console.log("error");
}
);
}
});

关于javascript - 在 AngularJS 中使用 json 数据时出现身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46726512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com