I get the 500 server error when trying to run my AJAX. I am very new to AJAX. Every thing works in the code if I run no AJAX in the script, for example just running:
$("#book-appointment-form").submit();
Therefore, it appears that all the database functions are fine. However, I need AJAX to run my code in a Wordpress page.
I do not see any notes in the error logs. The console log shows that the url is pointing to the correct location. What may I be missing?
The console log shows data within the hidden input showing up in confirmedData:
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 have tried replacing serializeArray()
with serialize()
. I have also tried serializeArray()
converted with $.param(confirmedData)
-- same results really and still it does not appear to reach the server. 500 error persists. I think serialize()
may be the more appropriate one however.