Reputation: 13
The idea is to do something similar to add-to-cart inside the Admin Dashboard of Opencart 2.1.0.2.
I've written my AJAX script of add some content to the table on click of a button. But clicking on the button gives an alert with the below response. I tried everything I could and even looked up tens of links but I just can't seem to find let alone resolve the issue. I even tried including the token in the url but it just doesn't work.
Any help will be highly appreciated. Thanks in advance.
Error Response
SyntaxError: Unexpected token <
OK
The above is then followed by the HTML script of the login page that also contains the error message of invalid token.
Ajax script
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);
}
});
Controller
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));
}
}
Model
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;
}
}
Upvotes: 0
Views: 2361
Reputation: 1430
You must add token to your url string.
url: 'index.php?route=stylist_dashboard/bucket/add&token=<?php echo &token ?>',
and define it in your controller file
$data['token'] = $this->session->data['token'];
Upvotes: 0
Reputation: 760
Set the contentType: "application/json",
in your code.
Example :
$.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);
}
Upvotes: 0