I’m trying to use the jquery .load() function to make a POST AJAX request that passes some values and returns some data.
.load() uses POST when the passed data is sent as an object:
$('#mydiv').load('remotescript.json', { 'key':'value', 'key2':'value2' }, 'callbackFunction');
Problem is, I want to write a function where I don’t hard-code the key names, but rather pass them in as a variable:
var field = $(this).parent().parent().find('.formfield');
var fn = field.attr('name');
var fv = field.val();
$('#mydiv').load('remotescript.json', { fn:fv }, 'callbackFunction');
For whatever reason, this doesn’t work. The variable name (fn) gets passed along literally as the key name, rather than the variable’s value getting passed. On the right side of the colon, the *value* of fv gets passed along just fine. It seems like whatever is entered on the left side of the colon gets treated as a string, whether it is quoted or not.
Does anyone know how to accomplish what I’m trying to do? Is there a jquery function that will create the object notation I want and allow me to pass in a variable’s value as the key name?