I get a 500 server error when trying to start my AJAX. I am very new to AJAX. Everything works in the code, if I do not run AJAX in a script, for example, I just run:
$("#book-appointment-form").submit();
Therefore, it seems that all database functions are wonderful. However, I need AJAX to run my code on a Wordpress page.
I do not see any notes in the error logs. The console log shows that the URL indicates the correct location. What can I lose?
The console log displays data in a hidden entrance, displayed in the confirmed data:
0: Object name: "csrfToken" value: "0f4343dfd0e71a8fa515d08f340f7bc9" __proto__: Object 1: Object name: "post_data" value: "{"customer":{"last_name":"Test","first_name":"Joe","email":" email4me@verizon.net ","phone_number":"9093334444","address":"","city":"","zip_code":"","id_cellcarrier":"2","wp_id":"1"},"appointment":{"start_datetime":"2015-12-25 11:00:00","end_datetime":"2015-12-25 11:50:00","notes":"","is_unavailable":false,"id_users_provider":"85","id_services":"13"},"manage_mode":false}" __proto__: Object length: 2 __proto__: Array[0]
VIEW:
<html> <form id="book-appointment-form" style="display:inline-block" method="post"> <button id="book-appointment-submit" type="button">Confirm</button> <input type="hidden" name="csrfToken" /> <input type="hidden" name="post_data" /> </form> </html>
Js
<script> $("#book-appointment-form").submit(function(event){ var confirmedData = $(this).serializeArray(); var dataUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_confirm_appointment'; $.post(dataUrl, confirmedData, function(response) { //////////////////////////////////////////////////////////// console.log('Customer Confirmed Post Response:', response); //////////////////////////////////////////////////////////// }, 'json'); event.preventDefault(); }); $("#book-appointment-form").submit(); </script>
PHP CONTROLLER
<?php public function ajax_confirm_appointment() { if($_POST["post_data"]){ try { $post_data = json_decode($_POST['post_data'], true); $appointment = $post_data['appointment']; $customer = $post_data['customer']; ...some database stuff here .... } catch(Exception $exc) { $view['exceptions'][] = $exc; } $this->load->view('appointments/book_success', $view); $form_data = TRUE; break; } else { $form_data = FALSE; } echo json_encode($form_data); } ?>
I tried replacing serializeArray() with serialize() . I also tried converting serializeArray() with $.param(confirmedData) - the same as it really is and it does not seem to reach the server. 500 is saved. I think serialize() might be more appropriate.