Reputation: 165
I'm trying to send some data via ajax with jquery
var name = $(".name").attr("data-name");
var value = $(".value").attr("data-value");
$.ajax({
url: 'panel.php',
type: 'post',
data: {name: value}
}).done(function(){
alert("saved!");
});
So how can name and value be two variables. Now only value is a variable but what about name?
Cheers
Upvotes: 4
Views: 20255
Reputation: 2656
try this:
var name = "data-name";
var value = "data-value";
var dataObj = {};
dataObj[name]=value;
$.ajax({
url: 'panel.php',
type: 'post',
data: dataObj,
}).done(function(){
alert("saved!");
});
Upvotes: 14
Reputation: 7600
You need to wrap it to a DTO (Data Transfer object):
var obj = {};
obj.name = name;
obj.value = value;
//Convert to a DTO Object
var dto = { 'myData': obj };
Upvotes: 1