Home > Mobile >  angular http.post<any> call not redirecting on correct url too
angular http.post<any> call not redirecting on correct url too

Time:01-13

unsupported Media TypeCould anyone help me to fix the http post issue, for some reason the url is not getting validated correctly, when the localhost:8084 is up and running, the error section gets executed and when the localhost:8084 is down even then the error section gets executed. I want the alert to show as successful when localhost:8084 is up and alert should show "email service is down" when the localhost:8084 is down. not sure what could be wrong here with the post link. any assistance is highly appreciated.

when the localhost is down, in the console log i see as status error = [object Object] " POST http://localhost:8084/mail net::ERR_CONNECTION_REFUSED", which is correct and as expected since the app is down. However, localhost:8084 is up, i just see this: error: status error = [object Object].

this.http.post<any>(http://localhost:8084/mail',mailData).subscribe(data =>
        {
          console.log('Successfully!');
          alert('\n\nYour Enquiry is Submitted Successfully')

        },
        error =>
        {
           console.log("status error = "   error);
           this.alert=true;
           alert('Email Service is down, sorry for inconvenience caused')
           //window.location.reload();
        }
      )

CodePudding user response:

subscribe() method contains two callback functions, the next() function is called if the request is successful and the error() function is called if the request fails

this.http.post<any>('http://localhost:8084/mail',mailData).subscribe(
    {
      next: data => {
        console.log('Successfully!');
      },
      error: error => {
        this.errorMessage = error.message;
        console.error('There was an error!', error);
      }
  }
  )

CodePudding user response:

After researching alot on the internet, i was able to get the issue fixed. Below import and inputs i had to include to make it work successfully. Adding here so that if other face same issue, they can have a look at this to fix the issue.

import { HttpHeaders, HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';

let httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), responseType: 'text' as 'json' };

this.http.post(environment.apiBaseUrl '/mail',mailData,httpOptions).subscribe

  •  Tags:  
  • Related