[Vuejs]-Intercept Promise error with vue-resource

0👍

I think I’d had the same issue, so I tried to look at the documentation. Well, it is very simple, you just need to do something like this:

In main.js

Vue.http.interceptors.push(function(req) {

//Here you can add some headers, if needed
req.headers.set('awesomeHeader', 'owwnt')

return function(res) {

    if( res.status == 200 || res.status == 201 || res.status == 202 ){ //Here you add the status codes that you'll work with, just like my example
        //Sucess response
    } else {
        //Every time witch an request return a status differ form the list above, you can do whatever you want, for example you can redirect the page for a new one
        window.location.href = `http://localhost:8080/#` 
        //If you want to display a notification, you need to import the component before, and then do something like it:
        Notification.success({
             title: 'error',
             message: 'Unauthorized request!',
             offset: 100
         })
    }

};
})

Does it answer you question? I hope it does.

Leave a comment