Переписываю запросы с jquery на нативный js и столкнулась с такой проблемой, что в ответ на запрос я получаю не малую корзину, а целиком страницу полной корзины.
Код в ТДС корзины
if (Core_Array::getRequest('add'))
{
$shop_item_id = intval(Core_Array::getRequest('add'));
if ($shop_item_id)
{
$oShop_Cart_Controller = Shop_Cart_Controller::instance();
$oShop_Cart_Controller
->shop_item_id($shop_item_id)
->quantity(floatval(Core_Array::getRequest('count', 1)))
->add();
}
}
// Ajax
if (Core_Array::getRequest('_', FALSE) && (!is_null(Core_Array::getRequest('add')) || !is_null(Core_Array::getRequest('loadCart'))))
{
ob_start();
// Краткая корзина
$Shop_Cart_Controller_Show = new Shop_Cart_Controller_Show(
$oShop
);
$Shop_Cart_Controller_Show
->xsl(
Core_Entity::factory('Xsl')->getByName(
Core_Array::get(Core_Page::instance()->libParams, 'littleCartXsl')
)
)
->couponText(Core_Array::get($_SESSION, 'coupon_text'))
->show();
echo json_encode(ob_get_clean());
exit();
}
Мой JS
const ajaxSend = ({ url, formData, successHandler, failHandler, method = 'GET' }) => {
if (!successHandler) {
alert("Callback function is undefined");
return;
}
const currentPath = url;
const currentData = Boolean(formData) ? formData : {};
console.log(formData, currentData)
return fetch(currentPath, {
method: method,
body: JSON.stringify(formData),
})
.then(response => {
if (response.ok) {
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return response.json().then(data => {
successHandler(data)
});
} else {
return response.text().then(text => {
successHandler(text);
});
}
} else {
if (failHandler) failHandler();
}
})
.catch(err => {
// любой обработчик ошибок на ваше усмотрение
console.log(err);
if (failHandler) failHandler();
})
}
async function addIntoCartCallbackFn(data) {
const container = document.querySelector('.head-cart');
container.innerHTML = data;
};
async function addIntoCartFn(path,elt) {
const shop_item_id = elt.dataset.itemId;
const count = elt.closest('.js-prod').querySelector('.qty__input').value;
if(!count) throw new Error('Нет элемента добавления в корзину');
ajaxSend({
url:`${path}?add=${shop_item_id}&count=${count}&loadCart=1`,
formData: {_: Math.round(new Date().getTime()).toString(), loadCart: true},
successHandler: addIntoCartCallbackFn,
// failHandler,
method: 'POST',
})
return false;
};
Оригинальный JS
clientRequest: function(t) {
void 0 === t.callBack && alert("Callback function is undefined"), e.loadingScreen("show");
var o = t.path,
n = void 0 !== t.data ? t.data : {};
return n._ = Math.round((new Date).getTime()), ({
context: t.context,
url: o,
type: "POST",
data: n,
dataType: "json",
success: t.callBack
}), !1
}
addIntoCart: function(path, shop_item_id, count){
$.clientRequest({
path: path,
data: {add: shop_item_id, count: count},
callBack: $.addIntoCartCallback,
context: $('#little-cart')
});
return false;
},
addIntoCartCallback: function(data, status, jqXHR)
{
$.loadingScreen('hide');
$(this).replaceWith(data);
},
Как получить малую корзину через fetch?