gpt4 book ai didi

javascript - AngularJS Promises 返回错误但成功将内容发送到数据库

转载 作者:行者123 更新时间:2023-12-03 05:15:43 26 4
gpt4 key购买 nike

我使用error.status在控制台日志中得到的错误是-1,但我发送到数据库的数据已成功发布。数据显示在我的 ng-repeat 列表中。这很奇怪,因为这种情况发生的概率为 5%。

这是我的 Controller :

    angular.module('app')
.controller('WebsiteCtrl', ['WebsiteService', '$scope', '$http', '$timeout', '$uibModal', function (WebsiteService, $scope, $http, $timeout, $uibModal, $uibModalInstance) {
$scope.model = WebsiteService.getWebsites();
$scope.clients = WebsiteService.getClients();
$scope.new = {
Website: {}
};

//create alerts when page reloads after crud functions
if (localStorage.getItem("Success")) {
$scope.alert = "Success";
$scope.alertMessage = localStorage.getItem("Success");
localStorage.clear();
} else if (localStorage.getItem("Error") && localStorage.getItem("Error") != null) {
//sometimes get errors even when adding, deleting, updating is successful
$scope.alert = "Error";
$scope.alertMessage = localStorage.getItem("Error");
localStorage.clear();
};


//Obtains list of websites from WebsiteSvc
getWebsites(); // this initializes Website List on page load
function getWebsites() {
WebsiteService.getWebsites()
.then(
function (data) {
$scope.model = data;
},
function (errResponse) {
console.log("Error while getting websites");
});
}

getClients();
function getClients() {
WebsiteService.getClients()
.then(
function (data) {
$scope.clients = data;
},
function (errResponse) {
console.log("Error while getting Clients");
});
}

//add a website
$scope.addWebsite = function () {
var data = WebsiteService.addWebsite($scope.new.Website).then(
function (success) {
localStorage.setItem("Success", "Added Website Id:" + success.Id + " Successfully!");
},
function (error) {
console.log(JSON.stringify(error, null, 2));
console.log(error.statusText);
localStorage.setItem("Error", "Error while adding website! " + error.status + ":" + error.statusText);
}
);
//location.reload();
}

//update a job
$scope.updateWebsite = function (website) {
WebsiteService.updateWebsite(website).then(
function (success) {
localStorage.setItem("Success", "Updated Website Id:" + success.Id + " Successfully!");
},
function (error) {
localStorage.setItem("Error", "Error while updating website! " + error.status + ":" + error.statusText);
console.log(error);
}
);
location.reload();
}
// deletes website
$scope.deleteWebsite = function (website) {
var indx = $scope.model.websiteList.indexOf(website);
WebsiteService.deleteWebsite(website.Id).then(
function (success) {
localStorage.setItem("Success", "Deleted Website Id:" + success.Id + " Successfully!");
},
function (error) {
localStorage.setItem("Error", "Error occured while deleting website! " + error.status + ":" + error.statusText);
}
);
location.reload();
}

//toggles active
$scope.updateActive = function (w) {
if (w.Active === true) {
w.Active = false;
} else if (w.Active === false) {
w.Active = true;
}
//updates job to reflect gui (active or inactive)
this.updateWebsite(w);
}

//select job
$scope.selectWebsite = function (w) {
$scope.selectedWebsite = w;
}

//show modal function
$scope.showModal = function (action, obj) {
$scope.showBool = true; //boolean to be able to exit modal after update
$scope.model.runButtonText = "Run Job"; //this is for run job only
$scope.$modalInstance = $uibModal.open({
scope: $scope,
inputs: {
title: action + " Job"
},
restrict: "E",
templateUrl: 'app/modal/WebsiteModals/modal' + action + 'Template.html',
controller: "JobCtrl",
backdrop: 'static',
keyboard: false

});
}

//exit modal function
$scope.exitModal = function () {
$scope.$modalInstance.dismiss('cancel');
};


}]);

我的服务:

    //Website Service class that executes the CRUD functionality
angular.module('app').factory('WebsiteService', ['$http', '$q', function ($http, $q) {

var factory = {
getWebsites: getWebsites,
getClients: getClients,
addWebsite: addWebsite,
updateWebsite: updateWebsite,
deleteWebsite: deleteWebsite,
};

return factory;

//creates service call for getWebsites
function getWebsites() {
var deferred = $q.defer();
$http.get('Websites/GetWebsites')
.then(
function (response) {
deferred.resolve(response.data);
},
function (errResponse) {
console.log('Error while getting websites');
deferred.reject(errResponse);
}
);
//returns a promise that function was successful or failed
//helps control asynch calls
return deferred.promise;
};

//creates service call for getClients
function getClients() {
var deferred = $q.defer();
$http.get('Websites/GetClients')
.then(
function (response) {
deferred.resolve(response.data);
},
function (errResponse) {
console.log('Error while getting websites');
deferred.reject(errResponse);
}
);
//returns a promise that function was successful or failed
//helps control asynch calls
return deferred.promise;
};

function addWebsite(website) {
var deferred = $q.defer();
console.log('Service: ', website);
$http.post('Websites/Post', website)
.then(
function (response) {
deferred.resolve(response.data);
console.log(response.data);
},
function (errResponse) {
console.log("Error while adding website");
deferred.reject(errResponse);
}
);
//returns a promise that function was successful or failed
//helps control async calls
return deferred.promise;
};

function updateWebsite(website) {
var deferred = $q.defer();
$http.post("Websites/Post", website)
.then(
function (response) {
deferred.resolve(response.data);
console.log(response.data);
},
function (errResponse) {
console.log("Error while updating website");
deferred.reject(errResponse);
}
);
//returns a promise that function was successful or failed
//helps control async calls
return deferred.promise;
};

function deleteWebsite(id) {
var deferred = $q.defer();
console.log('Service: ', id);
$http.post("Websites/Delete/" + id)
.then(
function (response) {
deferred.resolve(response.data);
console.log(response.data);
},
function (errResponse) {
console.log("Error while deleting website");
deferred.reject(errResponse);
}
);
//returns a promise that function was successful or failed
//helps control async calls
return deferred.promise;
};

}]);

它发生在所有与数据库相关的函数上,但我正在使用我的 addWebsite 函数来获取有用的数据。当这种情况很少发生时,很难进行调试。

任何帮助将不胜感激!提前致谢!

编辑这是错误为 -1 并显示网络选项卡的图片。这是在不同的选项卡中,但它是相同的代码。 enter image description here

最佳答案

根据 angularjs 文档:“200 到 299 之间的响应状态代码被视为成功状态,并将导致成功回调被调用。任何超出该范围的响应状态代码都被视为错误状态,并将导致调用错误回调。此外,小于 -1 的状态代码被标准化为零。-1 通常意味着请求被中止,例如使用 config.timeout。请注意,如果响应是重定向,XMLHttpRequest 将透明地跟随它,这意味着结果(成功或错误)将由最终响应状态代码确定。”查看:https://docs.angularjs.org/api/ng/service/ $http。

关于javascript - AngularJS Promises 返回错误但成功将内容发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41616782/

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