I am trying to do something similar to the following problem: ASP.NET: jQuery AJAX 'data' param problem.
My code looks like this:
var key = jQuery(this).parent().parent().attr('id');
var value = 1;
var object = {};
object[key]=value;
jQuery.ajax({
type: "POST",
url: "",
data: object
}).done(function() {
alert("message");
});
This does nothing at all. I know this should be simple, but I’m not getting anything. Still very new to jQuery and Javascript/AJAX. I have narrowed the problem down to the data object.
For example, if I do it this way
data: {1:1}
data: {2:1}
data: {3:1} etc.
it works, but I need my key value to be variable based on the ID of the element referenced, as opposed to constant.
To summarize, I am looking for a way to have a variable key in the AJAX key/value pairs.
Thanks for your help!
Ensure that the
keyyou’re getting is not null or empty before using it.Then to populate the object you could try
Then try handling the
successanderrormethods instead.