A
A
aftemaht2021-06-25 15:05:51
PHP
aftemaht, 2021-06-25 15:05:51

Why does Unexpected token in JSON at position 0 parsererror occur?

When placing an order and choosing a payment method on delivery, the following error appears.

Mistake
60d5bcc8e57bb143998370.png

Most likely the problem occurs in this section of code.
js
$('body').on('click', '#go_out', function(){

        var type_pay = $('#go_out').data('pay');
        $.post('/index.php?route=checkout/payment_method/save', {'payment_method' : type_pay, 'agree': 1, 'comment': ''}, function(){

        });
        $.get('/index.php?route=checkout/confirm', function(data){

            $('#user_confirm').html(data);
            // Если оплата при получении
            if(type_pay == 'cod'){
                $.ajax({
                    url: 'index.php?route=extension/payment/cod/confirm',
                    dataType: 'json',
                    beforeSend: function() {
                        $('#button-confirm').button('loading');
                    },
                    complete: function() {
                        $('#button-confirm').button('reset');
                    },
                    success: function(json) {
                        if (json['redirect']) {
                            location = json['redirect'];
                        }
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
            // Если оплата онлайн
            if(type_pay == 'anyway'){
                $('#user_confirm form .buttons .pull-right input').click();
            }

        });
    });

I also attach the code from the cod.php file
code.php
<?php
class ControllerExtensionPaymentCod extends Controller {
  public function index() {
    return $this->load->view('extension/payment/cod');
  }

  public function confirm() {
    $json = array();
    
    if ($this->session->data['payment_method']['code'] == 'cod') {
      $this->load->model('checkout/order');

      $this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('payment_cod_order_status_id'));
    
      $json['redirect'] = $this->url->link('checkout/success');
    }
    
    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));		
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-06-25
@aftemaht

You have an error because of Notice. Don't display PHP errors in the browser (display_errors = Off) to begin with. And deal with the notification why there was no payment_method in the session

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question