簡體   English   中英

在Opencart Admin中對自定義控制器的Ajax調用引發無效令牌錯誤

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

這個想法是做一些類似於Opencart 2.1.0.2的Admin Dashboard內的添加到購物車的事情。

我已經編寫了AJAX腳本,只需單擊一個按鈕即可向表中添加一些內容。 但是單擊該按鈕將發出以下響應的警報。 我盡了一切努力,甚至查找了數十個鏈接,但似乎找不到解決該問題的方法。 我什至嘗試將令牌包括在url中,但它根本不起作用。

任何幫助將不勝感激。 提前致謝。

錯誤回應

SyntaxError: Unexpected token <
OK

然后,上面是登錄頁面的HTML腳本,該腳本還包含無效令牌的錯誤消息。

Ajax腳本

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: 'index.php?route=stylist_dashboard/bucket/add&token=<?php echo &token ?>',

並在您的控制器文件中定義

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM