我尝试将新的Fetch API用于联系表单,该表单应该提交表单并返回我可以通过appendChild
向用户显示的回复。
不幸的是,我没有收到返回并显示的字符串。我在响应对象中获得了200状态代码,但不是我想要的消息。
var form = document.getElementById('ajax-contact'),
formaction = form.getAttribute('action'),
formMessages = document.getElementById( 'form-messages' );
form.addEventListener('submit', function(event) {
event.preventDefault();
fetch(formaction, {
method: 'post',
body: new FormData(form)
})
.then(function(response) {
formMessages.classList.remove('error');
formMessages.classList.add('success');
var content = document.createTextNode(response);
formMessages.appendChild(content);
})
});
我的PHP表单处理程序响应部分:
if ($recipient) {
http_response_code(200);
echo "Message has been sent.";
} else {
http_response_code(500);
echo "Something went wrong.";
}
我不知道我在这里失踪了什么。谢谢你的帮助。