誰能幫我解決 http post 問題,由于某種原因,url 沒有得到正確驗證,當 localhost:8084 啟動并運行時,錯誤部分被執行,當 localhost:8084 關閉時,即使錯誤部分被執行。我希望當 localhost:8084 啟動時警報顯示為成功,并且當 localhost:8084 關閉時警報應顯示“電子郵件服務已關閉”。不知道帖子鏈接有什么問題。非常感謝任何幫助。
當 localhost 關閉時,在控制臺日志中我看到狀態錯誤 = [object Object] "POST http://localhost:8084/mail net::ERR_CONNECTION_REFUSED",這是正確的,因為應用程式已關閉,因此符合預期。但是,localhost:8084 已啟動,我只看到: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();
}
)
uj5u.com熱心網友回復:
subscribe() 方法包含兩個回呼函式,請求成功時呼叫 next() 函式,請求失敗時呼叫 error() 函式
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);
}
}
)
uj5u.com熱心網友回復:
在互聯網上研究了很多之后,我能夠解決這個問題。在匯入和輸入下面,我必須包含以使其成功運行。在這里添加,以便如果其他人面臨同樣的問題,他們可以看看這個來解決問題。
從'@angular/common/http'匯入{ HttpHeaders, HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse };
讓 httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), responseType: 'text' as 'json' };
this.http.post(environment.apiBaseUrl '/mail',mailData,httpOptions).subscribe
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417692.html
標籤:
