【问题标题】:ajax call to custom controller in Opencart Admin throws invalid token error在 Opencart Admin 中对自定义控制器的 ajax 调用会引发无效令牌错误
【发布时间】:2019-02-09 14:57:27
【问题描述】:

这个想法是在 Opencart 2.1.0.2 的 Admin Dashboard 中执行类似于 add-to-cart 的操作。

我编写了我的 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;
    }
}

【问题讨论】:

    标签: php jquery ajax opencart


    【解决方案1】:

    在您的代码中设置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);
            }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        并在你的控制器文件中定义它

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-16
          • 1970-01-01
          • 1970-01-01
          • 2017-01-08
          相关资源
          最近更新 更多