Reputation: 5448
var type = $(this).attr("data-type");
var typeId = $(this).attr("data-value");
if(type=="category") var typeVar="cat";
else if(type=="subcategory") var typeVar="subcat";
$.ajax({
url: 'admin-catalog/next',
type: 'POST',
dataType: 'json',
data: {typeVar: typeId, limit: 9},
success: newGalleryProducts
});
The key 'typeVar' in my 'data' key needs to be dynamic, i.e. it should not post 'typeVar' as the key, it should post either 'cat' or 'subcat'.
Anybody know how to do this?
Upvotes: 1
Views: 99
Reputation: 160853
var type = $(this).attr("data-type");
var typeId = $(this).attr("data-value");
if(type=="category") var typeVar="cat";
else if(type=="subcategory") var typeVar="subcat";
var post_data = {};
post_data[typeVar] = typeId;
post_data['limit'] = 9;
$.ajax({
url: 'admin-catalog/next',
type: 'POST',
dataType: 'json',
data: post_data,
success: newGalleryProducts
});
If you want to do it within the data
key, you can do such way:
data: function(){var o = {}; o[typeVar] = typeId; o['limit'] = 9;return o;}(),
Or like @archil metioned, use JSON.parse
method:
data: JSON.parse('{ "' + typeVar + '": "' + typeId + '", "limit": "9"}'),
Upvotes: 3
Reputation: 39501
It is possible to achieve that by using JSON.parse
$.ajax({
url: 'admin-catalog/next',
type: 'POST',
dataType: 'json',
data: JSON.Parse('{ "' + typeVar + '": "' + typeId + '", "limit": "9"}'),
success: newGalleryProducts
});
Upvotes: 1