Объект ajax отправляется на сервер [закрыто]Python

Программы на Python
Anonymous
Объект ajax отправляется на сервер [закрыто]

Сообщение Anonymous »

Я хочу отправить эти данные на этот view.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'})

Я хочу отправить данные бронирования на это электронное письмо с руководством.

Подробнее здесь: https://stackoverflow.com/questions/784 ... -to-server

Вернуться в «Python»