angularjs - Angular - How to properly handle an HTTP error from server? -


currently i've got this:

    $http({       method: 'post',       url: 'http://api-endpoint/somescript/',       data: formdata,       headers: {         'content-type': 'application/x-www-form-urlencoded'       }     })       .then(function (response) {         console.log(response);       }); 

if script on other end works out ok, then gets called. however, let's script on server end has sort of error that's not caught. if make error tossing in garbage call asdfasdf(), then function isn't called, , instead in browser console:

cross-origin request blocked: same origin policy disallows reading remote resource @ http://api-endpoint/somescript/. (reason: cors header 'access-control-allow-origin' missing). 

how catch in angular code can handle in user-friendly manner?

edit: not duplicate, specific angular.

the $q.then() method accepts 3 function parameters, first being handler successful callbacks, second being errors, , third being notify. $http leverages $q behind scenes thenable should behave documentation suggests.

to handle errors in above code, toss in additional function error handling such:

.then(function (response) {     console.log(response); }, function(response) {     console.log(response); }); 

feel free browse $q documentation, promise api, additional details.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -