Calling ajax in a nested function

Highlighted
Excursionist
14 2 1

I am trying to run $.ajax from inside of a function, but it does not seem to work. I get the error "POSTmyshopifysite.com /cart/update.js 404 (Not Found)" Here is my code

 

(function($) {
function add_security_deposit(deposit_quantity, variant_id, qtyInTheCart, cartUpdates, cartItems, container_size) {

cartUpdates = { variant_id : deposit_quantity }
var params = {
type: 'POST',
url: '/cart/update.js',
data: { updates: cartUpdates },
dataType: 'json',
success: function() { 
window.location.href = '/cart';
}
}
$.ajax(params);
}

if ({{deposit_quantity_4}}>0) {
add_security_deposit({{deposit_quantity_4}}, {{variant_id_4}}, qtyInTheCart_4, cartUpdates_4, cartItems, '4oz stop');
}

return;
}) (jQuery);

Interestingly, the ajax call works when I don't call it from within a function. For example, the below works. But I want to call ajax from within a function because I'll have to call that function several times. Why am I not able to call ajax within a nested function?

 

(function($) {
cartUpdates_4 = { {{ variant_id_4 }}: {{deposit_quantity_4}} } var params_4 = { type: 'POST', url: '/cart/update.js', data: { updates: cartUpdates_4 }, dataType: 'json', success: function() { window.location.href = '/cart'; } } $.ajax(params_4);
return;
}) (jQuery);

 

0 Likes