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

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

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:

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: 'OK', url: 'http://localhost:3000/send-email', ok: false, …}
error
: 
{error: SyntaxError: Unexpected token 'E', "Email sent"... is not valid JSON at JSON.parse (<anonymous>…, text: 'Email sent successfully'}
headers
: 
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
: 
"Http failure during parsing for http://localhost:3000/send-email"
name
: 
"HttpErrorResponse"
ok
: 
false
status
: 
200
statusText
: 
"OK"
url
: 
"http://localhost:3000/send-email"
[[Prototype]]
: 
HttpResponseBase

Here is my code:

this.emailService.sendEmail(this.name, this.email, this.message).subscribe(
      () => {
        // Email sent successfully
        console.log('Email sent!');
        // Display a success message to the user, or perform any other action
      },
      (error) => {
        // Error occurred while sending email
        console.error('Error sending email:', error);
        // Display an error message to the user, or perform any other action
      }
    );

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:

modal.component.ts:361 Error sending email: 
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: &#39;OK&#39;, url: &#39;http://localhost:3000/send-email&#39;, ok: false, …}
error
: 
{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;}
headers
: 
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
: 
&quot;Http failure during parsing for http://localhost:3000/send-email&quot;
name
: 
&quot;HttpErrorResponse&quot;
ok
: 
false
status
: 
200
statusText
: 
&quot;OK&quot;
url
: 
&quot;http://localhost:3000/send-email&quot;
[[Prototype]]
: 
HttpResponseBase

Here is my code:

this.emailService.sendEmail(this.name, this.email, this.message).subscribe(
      () =&gt; {
        // Email sent successfully
        console.log(&#39;Email sent!&#39;);
        // Display a success message to the user, or perform any other action
      },
      (error) =&gt; {
        // Error occurred while sending email
        console.error(&#39;Error sending email:&#39;, error);
        // Display an error message to the user, or perform any other action
      }
    );

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; }:

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:

确定