Отправка объекта ajax на сервер [закрыто]
я хочу отправить эти данные в views.py
var bookingData = {
name: name,
email: email,
contact: contact,
date: date,
shift: shift,
members: members
};
// Send the booking data to the server using AJAX
var xhr = new XMLHttpRequest();
xhr.open("POST", "/sendBookingData", true);
xhr.setRequestHeader("Content-Type", "application/json");
// Set the event handler before sending the request
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// Booking data successfully sent to the server
// You can display a confirmation message or take other actions here
alert("Booking successful!");
closePopup(); // Close the popup after successful booking
}
};
Этот проект связан с системой книжного гида, в которой пользователь вводит данные с помощью всплывающего окна и при отправке данных отправляет их на gmail гида
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def send_booking_data(request):
if request.method == 'POST':
# Extract data from POST request
contact = request.POST.get('contact')
date = request.POST.get('date')
email = request.POST.get('email')
members = request.POST.get('members')
name = request.POST.get('name')
shift = request.POST.get('shift')
# Construct the email body with booking data
email_body = f"New Booking Request\n\n"
email_body += f"Name: {name}\n"
email_body += f"Email: {email}\n"
email_body += f"Contact: {contact}\n"
email_body += f"Date: {date}\n"
email_body += f"Shift: {shift}\n"
email_body += f"Number of Members: {members}\n"
# Send email to the guide's email address
guide_email = 'jdurgesh2246@gmail.com' # Replace with the actual guide's email address
try:
# Code to send email
# For debugging, print a success message
print("Email sent successfully")
send_mail(
subject="New Booking Request",
message=email_body,
from_email='ticketlessentrysystem@gmail.com',
recipient_list=[guide_email],
)
return JsonResponse({'success': True})
except Exception as e:
# If sending email fails, print the error message
print("Error sending email:", e)
return JsonResponse({'success': False, 'error': str(e)})
return JsonResponse({'success': False, 'error': 'Invalid request method'})
я хочу отправить данные о бронировании на электронную почту этого гида.