I'm having a problem. I'm trying to make a $http call with the .then function insted .sucssess and .error. But although i change the url of the request to a non existing one, it is always executing the succes handler:
$http.get('data/myjson.json').then(onSuccess, onError);
onSuccess(data){};
onError(data){};
Also, the data parameter has a status of 404, indicating the failure, but the onError got never execute.
some body can explain how this really work?
thanks!
get(url) . then(function (response) { console. log('get',response) }) . catch(function (data) { // Handle error here });
The then() method takes two callback functions as parameters and is invoked when a promise is either resolved or rejected. The catch() method takes one callback function and is invoked when an error occurs.
The $location in AngularJS basically uses a window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS.
It is tempting to do too much work in the AngularJS controller. After all, the controller is where the view first has access to JavaScript via $scope functions. However, doing this will cause you to miss out on code sharing across the site and is not recommended by AngularJS documentation.
This code works for me:
$http.get('data/myjson.json').then(onSuccess, onError);
function onSuccess(data) {
}
function onError(data) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With