MikeiLL
MikeiLL

Reputation: 6550

Retrieve JSON from javascript to php (OpenCart controller)

There is a nice JSON object in console.log, but can't seem to retrieve it in controller.

        function checkOut(data) {
        $.ajax({
        url: 'index.php?route=sale/customer/checkout&token=<?php echo $token; ?>&customer_id=<?php echo $customer_id; ?>&products=' + data,
        type: 'post',
        dataType: 'html',
        data: 'products=' + encodeURIComponent(data), 
        cache: false,
        beforeSend: function() {

            $('.success, .warning').remove();
            $('#button-checkout').attr('disabled', true);
            $('#checkout').before('<div class="attention"><img src="view/image/loading.gif" alt="" /> <?php echo $text_wait; ?></div>');
        },
        complete: function() {
            $('#button-checkout').attr('disabled', false);
            $('.attention').remove();
        },
        success: function(html) {
            console.log(data);
            $('#checkout').html(html);
        }
    });
}

Sending data via get only seems to send a string, [object Object].

Change the dataType to json and all the html in result page disappears.

Experimented with $.parseJSON(data);, which returns null value in console.log.

What's the method to retrieve the data object so it can be json_decode($data, TRUE)d and available as a php array?

Upvotes: 0

Views: 975

Answers (1)

cracker
cracker

Reputation: 4906

Try This

$.ajax({
    url: 'index.php?route=sale/customer/checkout&token=<?php echo $token; ?>&customer_id=<?php echo $customer_id; ?>',
    type: 'post',
    dataType: 'json',
    data: {
        products: data
    },
    success: function (data) {
        alert(data);
    }
});

Upvotes: 1

Related Questions