The .success
and .error
methods ignore return values.
Also parameter values are case sensitive.
this.postdata=function(data){
//THIS
return $http.post(URL,data);
//return $http.post(URL,DATA);
}
Deprecation Notice1
The $http
legacy promise methods .success
and .error
have been deprecated. Use the standard .then
method instead.
$scope.submitdataPromise = function() {
return service.postdata($scope.formdata);
});
$scope.submitdataPromise
.then(function onFulfilled(response){
$scope.data = response.data;
//return data for chaining
return data;
}).catch(function onRejected(response){
console.log(response.status);
//throw to chain error
throw response;
});
The submitdataPromise
function immediately returns a promise. The promise is a container for future results of the XHR resolved either fulfilled or rejected. Those results can be retrieved by registering functions with the $q
service to be invoked upon resolution.
The response object has these properties:
- data –
{string|Object}
– The response body transformed with the transform functions.
- status –
{number}
– HTTP status code of the response.
- headers –
{function([headerName])}
– Header getter function.
- config –
{Object}
– The configuration object that was used to generate the request.
- statusText –
{string}
– HTTP status text of the response.
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.
-- AngularJS $http Service API Reference - General Usage