gpt4 book ai didi

javascript - 重定向到另一个页面会阻止函数返回它们的值

转载 作者:数据小太阳 更新时间:2023-10-29 04:31:43 27 4
gpt4 key购买 nike

我有一个登录页面,如果用户登录,我想将用户重定向到另一个 HTML 页面,我将在其中列出我从服务器获得的用户任务。

问题是:

即使我编写的函数正常工作并且后端 API 返回我想要的值(我可以在控制台上看到值的详细信息)当我使用重定向代码 $window.location.href = '../Kullanici/userPanel.html 页面在登录后立即重定向,由于某种原因我无法使用重定向后函数返回的值。不仅如此,我再也看不到控制台日志中返回值的详细信息。

这是我的代码:

Controller :

app.controller('myCtrl', ['$scope', '$http', '$window','$mdToast', 'userTaskList',
function ($scope, $http, $window, $mdToast, userTaskList) {
$scope.siteLogin = function () {

var userName = $scope.panel.loginUserName;
var password = $scope.panel.loginPassword;
var loginMember = { //JSON data from login form
K_ADI: $scope.panel.loginUserName,
PAROLA: $scope.panel.loginPassword
};
$http({
method: 'POST',
url: 'http://localhost:5169/api/Kullanicilar/KullaniciDogrula',
headers: {
'Content-Type': 'application/json'
},
data: loginMember

}).then(function successCallback(response) {

console.log("message sent", response);
$scope.data = response.data.error.data;
if ($scope.data === true) {//if username and password is correct

console.log("User exists");
userTaskList.showActiveTasks(userName)
.then(function (activeTaskResponse) {
var activeTasks = activeTaskResponse;
console.log("Active tasks (controller): ", activeTaskResponse);

userTaskList.showFinishedTasks(userName)
.then(function (finishedTaskResponse) {
var finishedTasks = finishedTaskResponse;
console.log("Finished tasks(controller): ", finishedTaskResponse);
$scope.getMessage();
$window.location.href = '../Kullanici/userPanel.html';
}, function (err) {
console.log(err);
});

}, function (err) {
console.log(err);
});

}

}, function errorCallback(response) {
console.log("Couldn't send", response);
});
}

那么是什么原因导致了这个问题,我该如何解决呢?

编辑:我嵌套了 .then 部分,但它无法正常工作并给出了 This value was just evaluated now 警告。所以我仍然无法在重定向的 HTML 页面上使用数据。

我还删除了工厂,因为它使代码看起来非常困惑,而且它可能不是问题的根源。

最佳答案

我会将您的两个函数嵌套在第一个 promise 中,然后在所有函数完成后重定向。有点像

app.controller('myCtrl', ['$scope', '$http', '$window','$mdToast', 'userTaskList',
function ($scope, $http, $window, $mdToast, userTaskList) {
$scope.siteLogin = function () {

var userName = $scope.panel.loginUserName;
var password = $scope.panel.loginPassword;
var loginMember = { //JSON data from login form
K_ADI: $scope.panel.loginUserName,
PAROLA: $scope.panel.loginPassword
};

$http({
method: 'POST',
url: 'http://localhost:5169/api/Kullanicilar/KullaniciDogrula',
headers: {
'Content-Type': 'application/json'
},
data: loginMember

}).then(function successCallback(response) {

console.log("message sent", response);
$scope.data = response.data.error.data;
if ($scope.data === true) {//if username and password is correct

console.log("User exists");
userTaskList.showActiveTasks(userName)
.then(function (res) {
var activeTasks = res;
console.log("Active tasks (controller): ", res);

userTaskList.showFinishedTasks(userName)
.then(function (res) {
var finishedTasks = res;
console.log("Finished tasks(controller): ", res);
$scope.getMessage();
$window.location.href = '../Kullanici/userPanel.html';
}, function (err) {
console.log(err);
});

}, function (err) {
console.log(err);
});

} else { //if username or password is wrong
$mdToast.show(
$mdToast.simple()
.textContent('Username or Password is wrong')
.position('right')
.hideDelay(3000)
);
}
}, function errorCallback(response) {
console.log("Couldn't send", response);
});
}
}
]);

关于javascript - 重定向到另一个页面会阻止函数返回它们的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51396636/

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