0

I am calling a service from an Angular application, the service sends an email.

The method in Angular that calls the service to send an email:

sendEmailRegister(url, email, language) {
  var headers = this.authorizationService.initParamAuthorization(true); // capture token
  headers["language"] = language;
  headers["Content-Type"] = 'application/x-www-form-urlencoded';
  let body = 'email='+encodeURIComponent(email);
  return this.httpClient.post<any>(url, body, { 'headers':headers  }).toPromise();  
}

Analyzing the service in the Chrome browser tab "Network" I see that the service returns a status 500 error and when I access the "Response" it gives me a 401 error.

The funny thing is that when I do a "Copy as curl (Windows)" from the "Network" tab and then import from Postman using the "Raw text" tab to generate the request, it works correctly and sends the mail.

Capture call with OPTIONS method:

enter image description here

What could be the problem? Thanks.

Eladerezador
  • 1,277
  • 7
  • 25
  • 48
  • 1
    maby angular send "OPTION" request before use POST and OPTION return an error? – Никита Середа Mar 17 '21 at 09:47
  • In the "Network" tab, I first see the request "OPTIONS" which returns a 200 and then the request is repeated with "POST" and it returns a status of 500 and a 401 in the response – Eladerezador Mar 17 '21 at 09:56
  • @НикитаСереда, I find it weird that I preflight "OPTIONS" because the request I make is of type "POST" and contains a content type "application/x-www-form-urlencoded" – Eladerezador Mar 17 '21 at 10:21
  • Curiously, the service sends the mail from a specific domain correctly, the "OPTIONS" request is not verified in the domain that works well, only the "POST" request is seen. When I try it from other domains or localhost it doesn't work and in this case if the "OPTIONS" check is performed – Eladerezador Mar 17 '21 at 10:40

1 Answers1

1

I think that the server doesn't have CORS enabled for http://localhost. Could test maybe with this extension installed? https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino

And if it works like this, could you maybe enable the Access control allow origin flags on the server?

And this seems to be the explanation why it works on Postman and not in browser:

alexander
  • 79
  • 1
  • 1
  • 6
  • Sorry I keep receiving the same error using the Chrome extension, if it is a Cors problem I do not understand why the error does not explicitly indicate it – Eladerezador Mar 17 '21 at 12:38