JQuery ajax data two variables

I am trying to send some data via ajax using 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, both name and value can be two variables. Now only the value is a variable, but what about the name?

Greetings

+4
source share
2 answers

try the following:

 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!"); });​ 
+14
source

You need to wrap it in a DTO object (data transfer object):

 var obj = {}; obj.name = name; obj.value = value; //Convert to a DTO Object var dto = { 'myData': obj }; 
+1
source

All Articles