1

I want to make some http requests and return always status 200, because if I return other status the browser console will log it, and I want to avoid that. I read about JSend, and I want to know if there is anyway to catch it response doing something like this:

this.http.post("/login", JSON.stringify(credentials))
  .map(result => result.json())
  .subscribe(
    data =>this.router.navigate(["home"]),
    () => this.router.navigate(["login"], {queryParams: {error: true}})
  );

I want to avoid asking with ifs to know the backend response, if there is any other good way to do this is welcome too.

Motomine
  • 4,205
  • 5
  • 20
  • 23
  • Using **Json Server** this can be done easily. Here is a [**medium post **](https://medium.com/codingthesmartway-com-blog/create-a-rest-api-with-json-server-36da8680136d) which will guide you through. – Aravind Jul 01 '17 at 17:24
  • Is not exactly what I was looking for because I have a backend made in Java, this one is which receives the requests and returns the json, then Angular 2 get the result and I need to know if it was an error or not (without using status codes because the browser console will log it and I don't want that) – Motomine Jul 01 '17 at 17:37

1 Answers1

0

Your syntax is wrong. You are missing error argument for the error handling method

this.http.post("/login", JSON.stringify(credentials))
  .map(result => result.json())
  .subscribe(
    data =>this.router.navigate(["home"]),
   ////////////////////////////////////////////////////////////////////
    (error) => this.router.navigate(["login"], {queryParams: {error: true}})
   ////////////   error is argument for error handler     //////////////
   ////////////////////////////////////////////////////////////////////
  );

Angular Official Document

Aravind
  • 40,391
  • 16
  • 91
  • 110
  • Without the argument it was working too, the problem is not that angular is not catching the error, it does, the problem is that is only catching it when the status code is not 200, and the console log that too (I don't want that) – Motomine Jul 01 '17 at 17:45
  • That's the way `http` request behaves in general. `200` means **success**. what else you are expecting? – Aravind Jul 01 '17 at 17:47
  • I know I know, what I want to know if there is anyway to know something like "success" or "error" withot doing if(data.success){...} else{...}, the promises do that but taking into account the status code, but has the problem mentioned above – Motomine Jul 01 '17 at 17:48
  • if you `subscribe` to the response, two possibilities `1.success` or `2.error` based on which call back function is executed you can find success or error.. Am I clear? – Aravind Jul 01 '17 at 17:50
  • yes, it what I'm doing now, but is there any way to execute one or the other without asking for the status? That's what I'm trying to avoid because the browser always logs for an error – Motomine Jul 01 '17 at 17:52
  • you can skip the error handler which will do nothing if there is an error. – Aravind Jul 01 '17 at 18:01