ajax 调用 Opencart Admin 中的自定义控制器会抛出无效令牌错误


ajax call to custom controller in Opencart Admin throws invalid token error

这个想法是在Opencart 2.1.0.2的管理仪表板中做一些类似于添加到购物车的事情。

我已经编写了我的 AJAX 脚本,通过单击按钮向表添加一些内容。但是单击该按钮会发出带有以下响应的警报。我尽我所能,甚至查找了数十个链接,但我似乎找不到,更不用说解决问题了。我什至尝试在 url 中包含令牌,但它不起作用。

任何帮助将不胜感激。提前谢谢。

错误响应

SyntaxError: Unexpected token <
OK

然后是登录页面的 HTML 脚本,该脚本还包含无效令牌的错误消息。

阿贾克斯脚本

var bucket = {
    'add': function(product_id, client_id, stylist_id) {
        console.log(product_id + " " + client_id + " " + stylist_id);
        $.ajax({
            url: 'index.php?route=stylist_dashboard/bucket/add',
            type: 'post',
            data: {
                'product_id' : product_id,
                'client_id' : client_id,
                'stylist_id' : stylist_id
            },
            dataType: 'json',
            success: function(json) {
                //$('.alert, .text-danger').remove();
                console.log('inside success');
                if (json['redirect']) {
                    location = json['redirect'];
                }
                if(json['success']) {
                    console.log(json['success']);
                },
                error: function(xhr, ajaxOptions, thrownError) {
                   console.log(thrownError + "'r'n" + xhr.statusText + "'r'n" + xhr.responseText);
            }
        });

控制器

class ControllerStylistDashboardBucket extends Controller{
    public function add(){
        $this->log->debug('inside function add');
        $this->load->Model('stylist_dashboard/bucket');
        if (isset($this->request->post['product_id'])) {
            $product_id = (int)$this->request->post['product_id'];
        } else {
            $product_id = 0;
        }
        if (isset($this->request->post['client_id'])) {
            $client_id = (int)$this->request->post['client_id'];
        } else {
            $client_id = 0;
        }
        if (isset($this->request->post['stylist_id'])) {
            $stylist_id = (int)$this->request->post['stylist_id'];
        } else {
            $stylist_id = 0;
        }
        $this->log->debug($product_id,$client_id,$stylist_id);
        $bucket_id = $this->model_stylist_dashboard_bucket->add($product_id, $client_id, $stylist_id);
        //return $bucket_id;
        $json = array();
        $json['success'] = 'Successfully added to client bucket with Bucket Id: ' . $bucket_id;
        $this->response->setOutput(json_encode($json));
    }
}

class ModelStylistDashboardBucket extends Model{
    public function add($product_id, $client_id, $stylist_id){
        $this->db->query("INSERT INTO " . DB_PREFIX . "customer_bucket (customer_id, stylist_id, product_id) VALUES ('" . $client_id . "','" . $stylist_id . "','" . $product_id . "')");
        $bucket_id = $this->db->getLastId();
        return $bucket_id;
    }
}

在代码中设置contentType: "application/json",

例:

    $.ajax({
        url: 'index.php?route=stylist_dashboard/bucket/add',
        contentType: "application/json",
        type: 'post',
        data: {
            'product_id' : product_id,
            'client_id' : client_id,
            'stylist_id' : stylist_id
        },
        dataType: 'json',
        success: function(json) {
            //$('.alert, .text-danger').remove();
            console.log('inside success');
            if (json['redirect']) {
                location = json['redirect'];
            }
            if(json['success']) {
                console.log(json['success']);
            },
            error: function(xhr, ajaxOptions, thrownError) {
               console.log(thrownError + "'r'n" + xhr.statusText + "'r'n" + xhr.responseText);
        }

在 OC 管理员中,您必须在 url 字符串中包含令牌。

您必须将令牌添加到 url 字符串中。

url: 'index.php?route=stylist_dashboard/bucket/add&token=<?php echo &token ?>',

并在控制器文件中定义它

$data['token'] = $this->session->data['token'];