收到错误消息,尽管电子邮件已成功发送 – Angular

huangapple go评论112阅读模式
英文:

Getting error message eventhough email is send successfully -Angular

问题

I send an email with my angular application.
Even though the e-mail is sent I always get the error.
More specifically I get this message:

  1. HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: 'OK', url: 'http://localhost:3000/send-email', ok: false, …}
  2. error
  3. :
  4. {error: SyntaxError: Unexpected token 'E', "Email sent"... is not valid JSON at JSON.parse (<anonymous>…, text: 'Email sent successfully'}
  5. headers
  6. :
  7. HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
  8. message
  9. :
  10. "Http failure during parsing for http://localhost:3000/send-email"
  11. name
  12. :
  13. "HttpErrorResponse"
  14. ok
  15. :
  16. false
  17. status
  18. :
  19. 200
  20. statusText
  21. :
  22. "OK"
  23. url
  24. :
  25. "http://localhost:3000/send-email"
  26. [[Prototype]]
  27. :
  28. HttpResponseBase

Here is my code:

  1. this.emailService.sendEmail(this.name, this.email, this.message).subscribe(
  2. () => {
  3. // Email sent successfully
  4. console.log('Email sent!');
  5. // Display a success message to the user, or perform any other action
  6. },
  7. (error) => {
  8. // Error occurred while sending email
  9. console.error('Error sending email:', error);
  10. // Display an error message to the user, or perform any other action
  11. }
  12. );

Is any way I can get the status of the error message? or fix the code?
Thank you!

英文:

I send an email with my angular application.
Even though the e-mail is sent I always get the error.
More specifically I get this message:

  1. modal.component.ts:361 Error sending email:
  2. HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: &#39;OK&#39;, url: &#39;http://localhost:3000/send-email&#39;, ok: false, …}
  3. error
  4. :
  5. {error: SyntaxError: Unexpected token &#39;E&#39;, &quot;Email sent&quot;... is not valid JSON at JSON.parse (&lt;anonymous&gt;…, text: &#39;Email sent successfully&#39;}
  6. headers
  7. :
  8. HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
  9. message
  10. :
  11. &quot;Http failure during parsing for http://localhost:3000/send-email&quot;
  12. name
  13. :
  14. &quot;HttpErrorResponse&quot;
  15. ok
  16. :
  17. false
  18. status
  19. :
  20. 200
  21. statusText
  22. :
  23. &quot;OK&quot;
  24. url
  25. :
  26. &quot;http://localhost:3000/send-email&quot;
  27. [[Prototype]]
  28. :
  29. HttpResponseBase

Here is my code:

  1. this.emailService.sendEmail(this.name, this.email, this.message).subscribe(
  2. () =&gt; {
  3. // Email sent successfully
  4. console.log(&#39;Email sent!&#39;);
  5. // Display a success message to the user, or perform any other action
  6. },
  7. (error) =&gt; {
  8. // Error occurred while sending email
  9. console.error(&#39;Error sending email:&#39;, error);
  10. // Display an error message to the user, or perform any other action
  11. }
  12. );

Is any way I can get the status of the error message? or fix the code?
Thank you!

答案1

得分: 1

以下是您要翻译的内容:

"It's looks like the response of your API (who send the email) is a plain text, not a JSON. So, in httpClient.post you need indicate Angular that you expect a string adding { observe: 'response' }:

this.httpClient.post(..your-url..,data,{observe:'response'}),

See the docs -it's related to get but it's the same when we want received a response from a post-

英文:

It's looks like the response of your API (who send the email) is a plain text, not a JSON. So, in httptClient.post you need indicate Angular that you expect a string adding { observe: &#39;response&#39; }:

  1. this.httpClient.post(..your-url..,data,{observe:&#39;response&#39;}),

See the docs -it's related to get but it's the same when we want received a response from a post-

huangapple
  • 本文由 发表于 2023年6月29日 17:16:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定