我正在嘗試使用 fetch 和 catch 方法將 JSON stringfy 請求發送到我執行某些功能的 PHP 文件?,F在我沒有收到任何錯誤,但我無法使該功能正常工作。 fetch('http://localhost/server/mail.php', { method: 'POST', redirect: 'follow', headers : { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify({ // Passes values to mail.php name: inputName, phone: inputPhone }), }) .then(response =>{ console.log(response); if(response === 'success'){ history.push("/start"); // Route to another page history.go(0); } }) .catch(function(err) { // Return error console.log(err); });如果我調試,console.log(response)我會收到包含以下內容的反應對象:響應 {type: "cors", url: "http://localhost/server/mail.php", redirected: false, status: 200, ok: true, bodyUsed: false, ...}最后,我使用 PHP Mailer 庫通過它發送郵件的 PHP 代碼,我之前在另一個項目中使用過相同的代碼,應該可以正常工作。<?phpuse PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\Exception;require './phpmailer/src/Exception.php';require './phpmailer/src/PHPMailer.php';require './phpmailer/src/SMTP.php';$name = $_POST['name'];$from = '[email protected]'$to = '[email protected]'; $message = $_POST['phone'];$subject = '????? ???';$mail = new PHPMailer();$mail->SMTPDebug = 0;$mail->CharSet = 'UTF-8';$mail->SMTPAuth = true;$mail->Host = "server270.web-hosting.com";$mail->Port = 465;$mail->Username = $to;$mail->Password = "*******";$mail->SMTPSecure = "ssl";$mail->setFrom($from);$mail->Subject = $subject;$mail->isHTML(true);$mail->Body = "?? ?????: " . $name . "\r\n <br/> ?????: " . $message;$mail->addAddress($to, 'Yotam Dahan');if(!$mail->Send()){ echo "Error sending: " . $mail->ErrorInfo;}else{ echo "success";}我的問題是 PHP 沒有發送電子郵件,我猜是這樣inputName并且InputPhone沒有正確傳遞,所以代碼沒有運行并且響應不是success應該的。如何通過 reactJS 中的 fetch & catch 向 PHP 發送正確的 POST 請求?
如何使用 fetch 和 catch 引用對 PHP 文件的反應
忽然笑
2023-03-03 14:58:26