Adrian
Adrian

Reputation: 2012

AJAX/ json returning null open cart

Hi Im attempting a simple ajax request but I keep getting a null value for json.

Here is my javascript...

<script>
$(document).ready( function() { 
$('#donate-box-submit').on('click', function() {

    var donate_code = $('#charity-campaign-code').val();
    var donate_amount = $('#charity-campaign-amount').val();
    $.ajax({
        url: 'index.php?route=donate/donatenow',
        type: 'post',
        data: {
            donate_code: donate_code,
            donate_amount: donate_amount
        },
        dataType: 'json',
        beforeSend: function() {

        },  
        complete: function() {

        },          
        success: function(json) {
        console.log(json);
            alert(json['test']);
        },
        error: function() {
        }
    });
   });

 });



</script>

and my php...

    public function donatenow() {

    $json = array(
    'test' => 'Output this text'        
    );


    $this->response->setOutput(json_encode($json));     
}

I have also tried echo json_encode($json); just to rule out any issues with that OpenCart function, but the same issue is still there.

Upvotes: 0

Views: 559

Answers (1)

Jay Gilford
Jay Gilford

Reputation: 15151

The problem is the route you are using to call the method. Not sure on exactly what class you are using as the controller, but there should be three parts to the route: route=aaa/bbb/donatenow where as you've got aaa/donatenow

Upvotes: 1

Related Questions