javascript - Why Success or Error callback function not executing properly for $http.post in angularjs -
i'm trying communicate server using following code.
$scope.save = function () { $scope.loading = true; if ($scope.rolesetupmodel.roleid === "") { // save //$scope.loading = true; $http.post('/securitymodule/rolesetup/addrole', json.stringify($scope.rolesetupmodel)) .success(function (response) { $scope.loading = false; $('#addmodal').modal('hide'); $scope.binddatatogrid(); alert("success"); toastr.success(response.message); }) .error(function (response) { $scope.loading = false; alert("fail"); toastr.error(response.message); }); } else { $http.post('/securitymodule/rolesetup/editrole', json.stringify(convarrtoobj($scope.rolesetupmodel))) .success(function (response) { $('#addmodal').modal('hide'); $scope.loading = false; toastr.success(response.message); $scope.binddatatogrid(); }) .error(function (data) { $scope.loading = false; toastr.error(response.message); }); } }
but i'm getting wired behavior. link being hit everytime, put debugger there, before completing request page getting kind of refreshed making model empty out entering success or error call back. know little promise object couldn't make sense. i'm doing wrong here?
my guess aren't binding $scope.save()
ng-submit
of <form>
, have being triggered submit button, or have not followed docs forms
if form set with:
<form name="formscopename" ng-submit="save()" novalidate>
your $http
should work fine. if have action
set on form angular interprets want submit without ajax nd use default browser process. make sure action
attribute doesn't exist.
also note there no reason stringify data yourself. done automatically $http
internally. causing error perhaps
you can pass in $event
protection , preventdefault. isn;t necessary however.
ng-submit="save($event)"
js
$scope.save = function (event){ event.preventdefult(); .....
Comments
Post a Comment