6

I want to send both the key and value to be dynamic(and the key is dynamic like coming from user input). Then how to send the request. I want something like this:

var requestString;

if(something)
   requestString = "something";
else
   requestString= "else";

    jQuery.ajax({
                        url: handlerUrl,
                        dataType: "json",
                        data: {
                            requestString: request.term
                        }
                    });

Here requestString is a variable and dynamically set. But for current code. the key is itself becoming "requestString" which was ought to be dynamic. How to do this?

soham
  • 1,508
  • 6
  • 30
  • 47
  • you can try this http://stackoverflow.com/questions/6114057/how-to-use-dynamic-data-name-with-jquery-post – Ajouve Apr 26 '13 at 09:45

2 Answers2

3

Use

jQuery.ajax({
           url: handlerUrl,
           dataType: "json",
           data: requestString + '=' + request.term
      });
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

create an object, push the dynamically generated key and value to it.. and pass that object as data in ajax..

try this

var requestString ;
 .....
dataString={};
dataString[requestString]=request.term

jQuery.ajax({
               url: handlerUrl,
               dataType: "json",
               data: dataString
          });
bipen
  • 36,319
  • 9
  • 49
  • 62