我有 Angular2+ 應(yīng)用程序,它使用 PHP 作為服務(wù)器端。我需要將數(shù)據(jù)發(fā)送到 PHP 腳本,它將處理數(shù)據(jù)并創(chuàng)建一個(gè)文件,然后將該文件發(fā)送到管理員的郵件并向前端響應(yīng)消息已發(fā)送(或在其他情況下出現(xiàn)錯(cuò)誤) 。這是我的一些代碼:Angular2+submit() { this.http.post('https://myWeb.site/script.php', this.dataService.sharedData).subscribe(response => { if (response == "Message sent!"){ //Do something } })}PHPheader("Access-Control-Allow-Origin: *");header("Access-Control-Allow-Headers: Content-Type, origin");require 'vendor/autoload.php';use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xlsx;use PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\Exception;$vars = get_angular_request_payload();function get_angular_request_payload() { return json_decode(file_get_contents('php://input'), true);}//(some data processing and .xlsx file configuration)$writer = new Xlsx($spreadsheet);$fileName = 'NewOrder.xlsx';$writer->save($fileName);$mail = new PHPMailer();$mail->isSMTP();$mail->Host = 'smtp.gmail.com';$mail->Port = 587;$mail->SMTPSecure = 'tsl';$mail->SMTPAuth = true;$mail->Username = "somemail@gmail.com";$mail->Password = "somepassword";$mail->setFrom('somemail@some.com', 'sale.some.com');$mail->addAddress('tomail@gmail.com', 'CompanyName');$mail->Subject = 'New order';$mail->Body = 'My Text';$mail->CharSet = 'UTF-8';$file_to_attach = './NewOrder.xlsx';$mail->AddAttachment( $file_to_attach , 'New.xlsx' );$mail->send();echo json_encode('Message sent!', true);//response問題是,有時(shí)它會(huì)向管理員發(fā)送郵件,但 Angular 沒有得到任何響應(yīng),有時(shí)它工作得很好。我嘗試在第一行移動(dòng)響應(yīng)(只是為了檢查它是否有效),但沒有幫助。也許有人有什么建議?
httpclient .post() 方法沒有響應(yīng)
桃花長(zhǎng)相依
2023-08-11 11:08:34