Alex
Alex

Reputation: 66

Opencart custom controller function not found

When i'll try to send POST request from jquery i was take - 404 not found

There my custom controller:

<?php
class ControllerCardsCards extends Controller {
    public function index() {
        $this->document->setTitle($this->config->get('config_meta_title'));
        $this->document->setDescription($this->config->get('config_meta_description'));
        $this->document->setKeywords($this->config->get('config_meta_keyword'));

        if (isset($this->request->get['route'])) {
            $this->document->addLink($this->config->get('config_url'), 'canonical');
        }

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        $this->response->setOutput($this->load->view('cards/registraion', $data));
        $this->load->model('cards/cards');
        $this->log->write('Controller main');

    }

    public function addcardquery($data){
        $this->log->write('fired addCardQuery function');
        $this->load->Model('cards/cards');
        $this->load->language('cards/cards');
        $temp = $this->model_cards_cards->insertCard($data);
    }

And my jquery function:

$(document).ready(function(){ 
    $("#regCardSubmit").click(function(event) { 
          event.preventDefault();
                var myObject = new Object();
                myObject.lastname = $('#card_reg_lastname').val();

                myObject.firstname = $('#card_reg_firstname').val();
                myObject.middlename = $('#card_reg_middlename').val();
                myObject.cardnum = $('#card_reg_card_num').val();
                myObject.birthday = $('#card_reg_birthday').val();
                myObject.phone = $('#card_reg_phone_num').val();
                myObject.email = $('#card_reg_email').val();

                var data = {"action": "addcardquery"};
                data = $(this).serialize() + "&" + $.param(data);
                var outjson = JSON.stringify(myObject);
                //alert(outjson);
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "index.php?route=cards/cards/addcardquery",
                    data: outjson,
                    success: function(data) { alert(outjson); },
                    error: function(xhr, ajaxOptions, thrownError) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
                });
                return false;
    });
});

function addcardquery always doest not fired, i was tryed to call from browser the path index.php?route=cards/cards/addcardquery but i was get all the same.

Upvotes: 0

Views: 703

Answers (3)

Mujahid Bhoraniya
Mujahid Bhoraniya

Reputation: 1584

First Fall you can replace

$this->load->Model('cards/cards'); 

To

$this->load->model('cards/cards');

second changes is here

$temp = $this->model_cards_cards->insertCard($data);

after add below code

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));

i thik is work fine.

Upvotes: 1

Jay Gilford
Jay Gilford

Reputation: 15151

I notice a couple of things that you have done wrong, but wouldn't expect to cause this issue.

  • $data shouldn't be in the function parameter - OpenCart doesn't work like that
  • $this->load->Model should be $this->load->model (note the lower case m)

VQMod/OCMod are unlikely to be the culprit here as someone has suggested, as they would need to actively be editing your file using their system. The one possible explanation I can think of is you have something like opcache enabled in php, and this needs disabling while you develop your code

Upvotes: 0

Dmitriy Zhuk
Dmitriy Zhuk

Reputation: 767

Check your OCMOD modifications. it seems you have everything correct, but it just is not being viewed by the framework.

go to system/storage/modification/catalog/controller/cards/cards.php and check if it is there (your storage folder could be moved above so check that too)

Upvotes: 0

Related Questions