Reputation: 33
I have a javascript file and i have two ajax calls in it on different buttons. I have a controller, Auction.php, in which i have the functions for both actions ( showPopup and askBidControl ), but for the second function, the ajax returns 404 not found. It doesn't even enter the initContent function. Here is a sample of the code:
class askbidAuctionModuleFrontController extends ProductControllerCore {
public function initContent(){
$this->ajax = true;
parent::initContent();
}
public function displayAjaxShowPopup(){
$type = Tools::getValue('type');
switch($type){
case 'bid':
break;
case 'ask':
break;
default:
return false;
}
$product = $this->getTemplateVarProduct();
ob_end_clean();
header('Content-Type: application/json');
$tpl = _PS_MODULE_DIR_.'askbid/views/templates/front/productPopUp.tpl';
$this->context->smarty->assign('product', $product);
$this->context->smarty->assign('type', $type);
$html = $this->context->smarty->fetch($tpl);
$this->ajaxDie(Tools::jsonEncode([
'askbid_popup' => $html,
'product' => $product,
]));
}
public function displayAjaxAskBidControl(){
$type = Tools::getValue('type');
$price = Tools::getValue('price');
ob_end_clean();
header('Content-Type: application/json');
die('saracie');
// $this->ajaxDie(Tools::jsonEncode(['trafalet']));
}
}
The JS:
$(document).ready(function(){
$('.askBidButton').on('click', function () {
let buttons_container = $("#askbid_buttons");
let data = {
'action': 'showPopup',
'id_product': buttons_container.data('id-product'),
'id_product_attribute': buttons_container.data('id-product-attribute'),
'type': $(this).val()
};
let url = buttons_container.data('popup-url');
$.post(url, data, null, 'json').then(function (resp) {
$('body').append(resp.askbid_popup);
let productModal = $(`#bidask-modal-${resp.product.id}-${resp.product.id_product_attribute}`);
productModal.modal('show');
productConfig(productModal);
productModal.on('hidden.bs.modal', function () {
productModal.remove();
});
}).fail((resp) => {
prestashop.emit('handleError', {eventType: 'clickQuickView', resp: resp});
});
return false;
});
$('body').delegate('#continue_place_askbid', 'click', function(e){
let type = $(this).val();
let price = $(this).parent().find('#askbid_price_input').val();
let data = {
'action':'askBidControl',
'type': type,
'price': price
};
let url = $(this).closest('.quickview').data('popup-url');
console.log(url);
console.log('trafalet');
$.ajax({
url: url,
data: data,
success: function(resp){
console.log('trafaleteeeee');
},
error: function(err){
console.log(err);
}
});
return false;
});
productPopUp.tpl is in views/templates/front/
I don't know exactly how to make the second ajax call work. It just gives 404 error.
UPDATE: It seems it doesn't even go through initContent function of Auction.php on the second ajax request ( askBidController ) and it has 2 calls, one for the url i have in ajax, that returns 302 found, and another call to index.php?controller=404.
Upvotes: 0
Views: 4226
Reputation: 3349
The ajax methods of the PrestaShop controllers require a precise syntax (to be called with the action param):
displayAjax
+ myCustomAction
in your case:
public function displayAjaxAskBidControl(){}
should works ;)
NOTICE:
the module controllers should ever extend the ModuleFrontController
class and not other controllers.
In your case in the first ajax call you give the id_product as a param, so the parent controller do the initContent
method, in the second call the productController doesn't find the id_product so it redirects
Upvotes: 1