我们的想法是在Opencart 2.1.0.2的Admin Dashboard中执行类似于add-to-cart的操作。
我已经编写了我的AJAX脚本,只需点击一下按钮就可以在桌面上添加一些内容。但是单击该按钮会发出以下响应的警报。我尽我所能,甚至查了几十个链接,但我似乎无法找到解决问题的方法。我甚至尝试在网址中包含令牌,但它只是不起作用。
任何帮助都将受到高度赞赏。提前谢谢。
错误回复
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;
}
}
答案 0 :(得分:0)
在代码中设置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);
}
答案 1 :(得分:0)
在OC管理员中,您必须在令牌字符串中包含令牌。
答案 2 :(得分:0)
您必须将令牌添加到您的URL字符串。
url: 'index.php?route=stylist_dashboard/bucket/add&token=<?php echo &token ?>',
并在您的控制器文件中定义
$data['token'] = $this->session->data['token'];