英文:
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: '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!
答案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: '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-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论