如何解决Javascript XHR Post 请求到 php 文件:加载资源失败:服务器响应状态为 404未找到
我正在尝试将 formData 发布到 PHP 文件。我不断收到以下错误"
加载资源失败:服务器响应状态为 404(未找到)
index.html 文件和 test.PHP 文件位于同一目录中。
我尝试将文件移动到其他文件夹。 我试过 ./ ../ http://localhost/... 等
javascript
f($key1,$hash{$key1},$key2,$hash{$key2},...)
html
const ctForm = document.getElementById('ctForm');
ctForm.addEventListener('submit',(e)=>{
e.preventDefault();
const data = new FormData(ctForm);
const xhr = new XMLHttpRequest();
xhr.open("POST","test.PHP",true);
xhr.send(data);
})
test.PHP
<form id="ctForm" class="contact-form">
<label>Your info:</label>
<div class="client-info">
<input type="text" name="firstname" placeholder="first name" required>
<input type="text" name="lastname" placeholder="last name" required>
<input type="e-mail" name="email" placeholder="e-mail" required>
<input type="phone" name="phone" placeholder="phone no" required>
<input type="hidden" name="subject" value="Estimate Request">
<textarea class="message" name="message" placeholder="Message"></textarea>
<button class="send" type="submit" value="Send">Send</button>
</div>
</form>
如果您能提供帮助,在此先感谢
解决方法
假设您使用的是本地主机,您的 URL 字符串应该是
http://localhost/{project_folder_name}/test.php
而不是像下面那样只有 test.php
。
xhr.open("POST","http://localhost/{project_folder_name}/test.php");
您可以忽略第三个参数,如mdn doc中所述,第三个参数默认为true
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。