删除订单后,OpenCart会抛出无效的令牌会话

时间:2017-04-25 09:39:18

标签: ajax opencart

我编写了一个定制的Ajax调用和函数来删除OpenCart 2.1.0.2中缺少的订单。虽然该功能正常工作,并且订单被删除,但从控制器返回后,OpenCart会将我注销并给出Invalid Token Session错误,即使传递的令牌是正确的。

这是我在order_list.tpl模板上的ajax调用:

$('#button-delete_missing').on('click', function(e) {
  if (confirm('<?php echo $text_confirm; ?>')) {
    var node = this;
    $.ajax({
      url: '<?php echo $store; ?>index.php?route=api/order/deleteMissing&token=<?php echo $_GET["token"] ?>',
      dataType: 'json',
      crossDomain: true,
      beforeSend: function () {
        $(node).button('loading');
      },
      complete: function () {
        $(node).button('reset');
      },
      success: function (json) {
        $('.alert').remove();

        if (json['error']) {
          $('#content > .container-fluid').prepend('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
        }

        if (json['success']) {
          $('#content > .container-fluid').prepend('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  }
});

这与页面上的其他调用完全相同,只是它在控制器中使用不同的功能:

public function deleteMissing() {
  $this->load->language('api/order');

  $json = array();

  $this->load->model('checkout/order');

  $this->model_checkout_order->deleteMissingOrders();

  $json['success'] = $this->language->get('text_success');

  if (isset($this->request->server['HTTP_ORIGIN'])) {
    $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
    $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    $this->response->addHeader('Access-Control-Max-Age: 1000');
    $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  }

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

反过来,它与删除功能相同,只是不需要特定的订单ID。这是模型中实际执行删除的功能:

public function deleteMissingOrders() {

  $query = $this->db->query("SELECT order_id FROM " . DB_PREFIX . "order WHERE order_status_id = 0 OR order_status_id = null");
  $rows = $query->rows;
  $order_ids = array();

  foreach($rows as $row) {
    $order_ids[] = $row['order_id'];
  }

  if(!empty($order_ids)) {
    foreach($order_ids as $order_id) {
      $this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");
      $this->db->query("DELETE FROM `" . DB_PREFIX . "order_product` WHERE order_id = '" . (int)$order_id . "'");
      $this->db->query("DELETE FROM `" . DB_PREFIX . "order_option` WHERE order_id = '" . (int)$order_id . "'");
      $this->db->query("DELETE FROM `" . DB_PREFIX . "order_voucher` WHERE order_id = '" . (int)$order_id . "'");
      $this->db->query("DELETE FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "'");
    }
  }
}

该函数获取所有空或零ID顺序状态并删除所有相关数据 - 此工作。我已经尝试过了,所有丢失的订单都被删除了;只是当它返回时它会抛出一个非描述性的错误(即不运行ajax调用的成功部分)并将我从管理区域中注销。

如果有人可以提供帮助,我将不胜感激。也许有一件事我错过了,或者我还没有把它放进去的东西?

1 个答案:

答案 0 :(得分:0)

我正确地阅读此内容,您的脚本似乎不知道令牌是什么。它有声明吗?因为Jquery不知道PHP知道什么。

执行类似

的操作

&#13;
&#13;
 // TODO: Should I make the list contain Video/Result objects and pull the data from that?
    @Inject
    public MainScreenRecyclerAdapter(List<Result> dataset, Context context) {
        myDataset = dataset;
        myContext = context;
    }
&#13;
&#13;
&#13;