How to catch (and display on the client) server-side OData business validation errors in JayData using AngularJs

StackOverflow https://stackoverflow.com/questions/19550010

  •  01-07-2022
  •  | 
  •  

Question

If I am using code such as this (from: http://jaydata.org/blog/jaydata-and-angularjs-continued):

$scope.saveChanges = function () {

$scope.northwind.saveChanges()
.then(function () {
  $scope.selectedProduct = null;
},function() {
  $scope.northwind.stateManager.reset();
});

};

How do I catch any server-side business validation errors that the server may return?

Was it helpful?

Solution

This works:

$scope.saveChanges = function () {
    $scope.ApplicationData.saveChanges()
    .then(function () {
        $scope.selectedToDo = null;
    }, function (error) {
        var xml = error.message,
        xmlDoc = $.parseXML(xml),
        $xml = $(xmlDoc),
        $ValidationResults = $xml.find("ValidationResults");

        angular.forEach($ValidationResults, function (ValidationResult) {
            angular.forEach(ValidationResult.childNodes, function (childNode) {
                alert(childNode.childNodes[0].textContent);
            });
        });

        $scope.ApplicationData.stateManager.reset();
    });
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top