0

I am working in Codeigniter. I have used my model in other coding. I know it works.

I am getting AJAX parsererror and Failure Handler at the point of running the model. It seems that JSON syntax is not processed correctly. When I echo back the data for $waitinglist on the php end I see this in my error message in the console:

"array(2) {↵ ["id_users_provider"]=>↵ string(2) "86"↵ ["notes"]=>↵ string(47) "emailaddress@verizon.net;9093334444@tmomail.net"↵}↵"

It does not look correct. I think it should have something like

"{"appointment":{"id_users_provider":"85","notes":"emailaddress@verizon.net;9093334444@tmomail.net"}}"

So where have I gone wrong?

PHP Controller

public function ajax_register_waiting(){
   try {
        $post_data = $_POST['post_data'];
        $waitinglist = $post_data['appointment'];

        $this->load->model('appointments_model');
        $this->appointments_model->waitinglist_to_db($waitinglist);

    } catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
    }
}

public function book_waiting(){
    $this->load->view('appointments/waiting_success');//return to book view
}

JS

$('#save-waitinglist').click(function(event) { 
if(validateWaitinglistEmail() && validateWaitinglistPhone()){

        postWaiting = new Object();
        var note = ''

        if($('#cell-carrier2').val() !== "" && $('#phone-number2').val() !== ""){
        note = $('#email2').val()  + ";" + $('#phone-number2').val().replace(/[^\d\+]/g,"") + $('#cell-carrier2').val();
        } else {
        note = $('#email2').val();
        }

        postWaiting['appointment'] = {
        'id_users_provider': $('#select-provider').val(),
        'notes': note,
        };

        $('input[name="csrfToken"]').val(GlobalVariables.csrfToken);
        $('input[name="post_waiting"]').val(JSON.stringify(postWaiting));       

        var formData = jQuery.parseJSON($('input[name="post_waiting"]').val());

        var postData = {
            'csrfToken': GlobalVariables.csrfToken,
            'post_data': formData,
        };

        var postUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_register_waiting'; 
        $layer = $('<div/>');

        $.ajax({
            url: postUrl,
            method: 'post',
            data: postData,
            dataType: 'json',
            beforeSend: function(jqxhr, settings) {
                $layer
                    .appendTo('body')
                    .css({
                        'background': 'white',
                        'position': 'fixed',
                        'top': '0',
                        'left': '0',
                        'height': '100vh',
                        'width': '100vw',
                        'opacity': '0.5'
                    });
            }
        })
        .done(function(response) {
            if (!GeneralFunctions.handleAjaxExceptions(response)) {
                return false;
            }
            window.location.replace(GlobalVariables.baseUrl
                + '/index.php/appointments/book_waiting');
        })
        .fail(function(jqxhr, textStatus, errorThrown) {
            GeneralFunctions.ajaxFailureHandler(jqxhr, textStatus, errorThrown);
        })
        .always(function() {
            $layer.remove();
        })
    }
}); 
Craig Tucker
  • 1,051
  • 1
  • 11
  • 27
  • Hmm `$waitinglist` is an array as expected since it is from a `POST`. I don't think that is where the error occurs. – missing_one Jan 12 '16 at 02:27
  • Yes but the array appears in the wrong format. I am not sure how to build it otherwise. When I remove the model from the code I have no errors but of course nothing posts. When I run a non-ajax post the model works fine. The difference I see is in the format of the array. – Craig Tucker Jan 12 '16 at 02:40
  • Can you show us your code on how you echoed / outputted your json encoded output? – vsogrimen Jan 12 '16 at 02:51
  • I added `$testit=var_dump($waitinglist); echo $testit;` just prior to the model. This produces the string listed above in AJAX Failure Handler console notice at the responseText. – Craig Tucker Jan 12 '16 at 02:58

1 Answers1

0

This helped: jQuery returning "parsererror" for ajax request

I removed the dataType: 'json', and all works now.

Community
  • 1
  • 1
Craig Tucker
  • 1,051
  • 1
  • 11
  • 27