AJAX提交和500服务器错误


AJAX submit and 500 server error

我在尝试运行AJAX时遇到500服务器错误。我是AJAX的新手。如果我在脚本中不运行AJAX,例如只运行,那么代码中的每一件事都会起作用

$("#book-appointment-form").submit();

因此,似乎所有的数据库功能都很好。然而,我需要AJAX在Wordpress页面中运行我的代码。

我在错误日志中没有看到任何注释。控制台日志显示url指向正确的位置。我可能缺少什么?

控制台日志显示隐藏输入中的数据,显示在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]

视图:

<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控制器

<?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);
    }
?>

我试着用serialize()替换serializeArray()。我还尝试过用$.param(confirmedData)转换serializeArray()——结果确实相同,但似乎仍然无法到达服务器。500错误仍然存在。不过,我认为serialize()可能更合适。

我认为它与Ajax无关。您通过ajax调用的脚本中可能存在问题。

尝试在没有ajax dataUrl 的情况下进行检查

还请检查链接。http://pcsupport.about.com/od/findbyerrormessage/a/500servererror.htm

这很有效:

我的JS

<script>
            $("#book-appointment-form").submit(function(event){
                    var postData = { 
                    csrfToken: $('input[name=csrfToken]').val(),
                    post_data: jQuery.parseJSON($('input[name="post_data"]').val())
                };
                var postUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_confirm_appointment'; 
                $.post(postUrl, postData, function(response) {
                ////////////////////////////////////////////////////////////
                console.log('Customer Confirmed Post Response:', response);
                ////////////////////////////////////////////////////////////
                if (!GeneralFunctions.handleAjaxExceptions(response)) return;
                }, 'json');
            });
</script>

我的控制器

<?php
public function ajax_confirm_appointment() {
    try {
        $post_data = $_POST['post_data'];
        $appointment = $post_data['appointment'];
        $customer = $post_data['customer'];
        ...some database stuff here ....
    }
    echo json_encode($yn_response);
}
?>

不再有500服务器错误。