Issue
So, I have this simple contact form, which Django handles by sending e-mails to me and to the person who contacted us. I've written an AJAX funcion to handle it. The only thing is that, no matter what I try, I keep getting an 500 status.
My Django (1.11) code:
def contato(request):
if request.method == 'POST':
name = request.POST['name']
email = request.POST['email']
phone = request.POST['phone']
message = request.POST['message']
newmessage = "Mensagem recebida de {}.\nTelefone: {}\ne-mail {}\n {}".format(name, phone, email, message)
send_mail(
'Nova mensagem pelo site',
newmessage,
'PLACEHOLDER@EMAIL',
['PLACEHOLDER@EMAIL'],
fail_silently=False
)
send_mail(
'Recebemos sua mensagem!',
'Olá, {}!\nRecebemos sua mensagem e entraremos em contato em breve.'.format(name),
'PLACEHOLDER@EMAIL',
[email],
fail_silently=False
)
return HttpResponse()
And here's my AJAX request:
form.addEventListener("submit", function(event) {
event.preventDefault();
let dadoscontato = {
name: name.value,
email: email.value,
phone: phone.value,
message: message.value,
csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value
};
fetch('/contato/', {
method: 'POST',
credentials: 'same-origin',
headers: {
"X-CSRFToken": getCookie("csrftoken"),
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(dadoscontato),
})
.then(function(response){
console.log(response)
return response.json();
})
.then(function(response){
alert('Mensagem enviada com sucesso!');
});
});
Solution
Just to mark this question as solved:
I had to run a .decode()
while loading the response. Everything works fine now.
Answered By - Moisés Almeida
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.