繁体   English   中英

任何 php 请求都返回 502 Bad Gateway

[英]Any php request return 502 Bad Gateway

我决定写一个小的 php 脚本来发送电子邮件。 我遇到了一个问题 - 当我从 PHPMailer 类通过$mail->send()发送请求时,我得到一个错误的网关 502。之后,脚本冻结并且不响应请求。 即使我更改了 emailSender.php 中的代码,也没有任何反应。 甚至 echo 命令也不起作用。

当我重新启动本地服务器时,脚本再次响应,但再次返回 502。 来自$_POST["EmailRecipient"]$_POST["Message"]请求的数据正确出现,并且也通过回声正确输出。 我可以在控制台中看到它。

然后我尝试在 google.com 上提出请求,再次得到 502 bad gateway。 我究竟做错了什么? 这是我的第一个 php 代码,我无法弄清楚。

我在 phpstorm 中运行了本地主机上一个文件夹中的所有文件。 请解释我做错了什么。 下面是我的代码。

 function processForm() { let emailRecipient = document.getElementsByName('EmailRecipient')[0].value; let emailMessage = document.getElementsByName('Message')[0].value; if (checkEmail(emailRecipient) && emailMessage) { let form = document.getElementById('emailForm'); let formData = new FormData(form); postRequest('emailSender.php', formData); return false; } else { showMessage('You have entered an invalid email address!'); return false; } } function postRequest(url, data) { fetch(url, { method: 'POST', mode: 'no-cors', cache: 'no-cache', body: data }) .then(handleErrors) .then(response => showMessage(response.text())) .catch(error => showMessage(error)); } function handleErrors(response) { if (!response.ok) { throw Error(response.statusText); } return response; } function showMessage(responseText) { console.log(responseText); } function checkEmail(email) { let regex = /^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/; return regex.test(email); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <form id="emailForm"> <div> <p>To:</p> <input type="text" name="EmailRecipient"> </div> <div> <p>Message:</p> <textarea name="Message"></textarea> </div> <div> <input type="submit" name="SendEmail" onclick="return processForm();"> </div> </form> </div> </body> <script src="formHandler.js"></script> </html>

<?php

$emailRecipient = '';
$message = '';

if (isset($_POST["EmailRecipient"]) && isset($_POST["Message"])) {
    $emailRecipient = $_POST["EmailRecipient"];
    $message = $_POST["Message"];

    echo $emailRecipient;
    echo "\n";
    echo $message;
}

$sFile = file_get_contents("http://www.google.com"); // 502 bad gateway

echo "\n";
echo $sFile; 

没有真正弄清楚服务器设置和phpstorm。 我认为它在安装过程中会自动拉起服务器及其配置。 尽管如此,他还是在没有请求的情况下在本地主机上运行了简单的脚本。 但是结果发现phpstorm需要手动配置。 到目前为止,我已经为自己找到了出路:使用服务器根文件夹中的项目。 就我而言,这是 XAMPP,路径是 C:\\xampp\\htdocs。 如果您从那里运行项目,那么请求将正确进行。 似乎您还可以从任意目录配置映射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM