javascript - 任何 php 请求都返回 502 Bad Gateway
问题描述
我决定编写一个小的 php 脚本来发送电子邮件。我遇到了一个问题 - 当我通过$mail->send()
PHPMailer 类发送请求时,我得到一个 Bad Gateway 502。之后,脚本冻结并且不响应请求。即使我更改了 emailSender.php 中的代码,也没有任何反应。甚至 echo 命令也不起作用。
当我重新启动本地服务器时,脚本再次响应,但再次返回 502。来自$_POST["EmailRecipient"]
and$_POST["Message"]
请求的数据是正确的,并且也可以通过 echo 正确输出。我可以在控制台中看到它。
然后我尝试在 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。我认为它会在安装过程中自动拉起服务器及其配置。尽管如此,他还是在没有请求的情况下在 localhost 上运行了简单的脚本。但事实证明phpstorm需要手动配置。到目前为止,我已经为自己找到了一条出路:使用服务器根文件夹中的项目。就我而言,这是 XAMPP,路径是 C:\xampp\htdocs。如果您从那里运行项目,那么请求会正确执行。似乎您也可以从任意目录配置映射。
推荐阅读
- java - 如何设置 CameraX (Android) 在 5 秒后停止录制?
- elasticsearch - How to get total number of aggregation buckets in Elasticsearch?
- c# - 如何利用来自 API 数据的数据库层次结构的实体框架
- node.js - 角度服务导致 EACCES:权限被拒绝 127.0.0.1:4201
- url - Instagram 基本显示返回带有错误 URL 时间戳消息的媒体 URL
- c# - PostgresException:42883:运算符不存在:@ 字符变化
- python - 在 Django 中添加动态电子邮件后端
- macos - 如何在 MacOS 11 Big Sur 上安装 brew httpd?
- symfony - Symfony - 有条件的嵌入式收集表单验证
- android - Android 谷歌地图 API 密钥“REQUEST_DENIED”