【发布时间】: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;
}
}
【问题讨论】: