2017-06-28 3 views
1

ich eine axios Anfrage habe in meiner reagieren Anwendung fangen und sich einzuloggen, für die ich die axios npm docs.Kann die Fehlerreaktion von einer axios Anfrage

folgende bin Dies ist mein axios anfordern

axios.post(helper.getLoginApi(), data) 
     .then((response) => { 
      console.log(response); 

      this.props.history.push(from.pathname) 
     }) 
     .catch((error)=> { 
      console.log(error); 
     }) 

Ich kann erfolgreich die Daten einer erfolgreichen Anfrage protokollieren. Jedoch, wenn ich absichtlich einen Fehler erzeugen und versuchen, es zu console.log, ich habe nicht das Ergebnis stattdessen angemeldet, ich jedoch nur

POST http://localhost:3000/login 401 (Unauthorized) :3000/login:1
Error: Request failed with status code 401 login.js:66

at createError (createError.js:16)

at settle (settle.js:18)

at XMLHttpRequest.handleLoad (xhr.js:77)

sehen, wenn ich auf Netzwerk-Tab in Chrome-Konsole gehen, ich das sehen kann unter Antwort zurückgegeben.

enter image description here

Vielen Dank für Hilfe im Voraus.

+0

Haben Sie versucht, Ihre axios in einen 'try {} catch (e) Umwickeln { } Aussage? Haben Sie versucht, den 'response.status' in' then' zu überprüfen? Nur ein paar Ideen - ich weiß nicht, warum es nicht funktioniert – lumio

+0

@lumio, wie ich schon sagte, in .dann kann ich 'response.data' erfolgreich protokollieren, aber nicht im' .catch' Block – Jagrati

+0

Obwohl Ich benutze Axios selbst. Ich bin mir nicht sicher, ob Axios immer fehlschlagen, wenn der Status nicht '2xx' ist. Haben Sie versucht, einen Fehler in "then" zu werfen, wenn "response.status" nicht 200 ist? – lumio

Antwort

5

Von der Github Docs. Die Antwort eines axios Anfrage sieht aus wie

{ 
    // `data` is the response that was provided by the server 
    data: {}, 

    // `status` is the HTTP status code from the server response 
    status: 200, 

    // `statusText` is the HTTP status message from the server response 
    statusText: 'OK', 

    // `headers` the headers that the server responded with 
    // All header names are lower cased 
    headers: {}, 

    // `config` is the config that was provided to `axios` for the request 
    config: {}, 

    // `request` is the request that generated this response 
    // It is the last ClientRequest instance in node.js (in redirects) 
    // and an XMLHttpRequest instance the browser 
    request: {} 
} 

So im Wesentlichen ist catch(error =>) eigentlich nur catch(response =>)

und so können Sie error.response.data anmelden und Sie sollten in der Lage sein, Ihre Antwortnachricht zu sehen.

When you log console.log(error) , what you see is the string returned by the toString method on the error object.

Nach der Fehlerbehandlung Abschnitt auf denselben Dokumenten, können Sie den Fang die Fehlerreaktion haben wie

axios.post(helper.getLoginApi(), data) 
     .then((response) => { 
      console.log(response); 

      this.props.history.push(from.pathname) 
     }) 
     .catch((error)=> { 
      if (error.response) { 
       // The request was made and the server responded with a status code 
       // that falls out of the range of 2xx 
       console.log(error.response.data); 
       console.log(error.response.status); 
       console.log(error.response.headers); 
      } else if (error.request) { 
       // The request was made but no response was received 
       // `error.request` is an instance of XMLHttpRequest in the browser and an instance of 
       // http.ClientRequest in node.js 
       console.log(error.request); 
      } else { 
       // Something happened in setting up the request that triggered an Error 
       console.log('Error', error.message); 
      } 
     }) 
+0

Vielen Dank für eine schnelle Antwort Ich werde versuchen, Sie wissen zu lassen. – Jagrati

+1

Danke Shubham, ich bekomme die Fehlermeldung. Danke auch dafür, warum es in meinem Fall nicht funktioniert hat –