views:

637

answers:

3

Hello, I have the following JSON object:

new Ajax.Request(url, {
   method: 'post',
   contentType: "application/x-www-form-urlencoded",
   parameters: {
          "javax.faces.ViewState": encodedViewState,
          "client-id": options._clientId,
          "component-value": options._componentValue
   }
});

Now, I would like to be able to append to the "Parameters" object programattically, but I am unsure of how I would actually do this.

+4  A: 

you can simply assign to it. But you might want to do that before creating the request.

var parameters = {
      "javax.faces.ViewState": encodedViewState,
      "client-id": options._clientId,
      "component-value": options._componentValue
}
parameters.foo = 'bar';

var myAjax = new Ajax.Request(url, {
    method: 'post',
    contentType: "application/x-www-form-urlencoded",
    parameters: parameters
});
Luke Schafer
just a note: another way to do the same thing is parameters["foo"] = "bar". I prefer this way because parameters.foo does not exist (yet), and it feels very "violating" to do the assignment that way :)
Here Be Wolves
except that it's exactly the same thing, and your method requires interpretation at run-time as opposed to parse-time (meaning a preparsed function can't work off the interpreted value)err.. did that make sense?
Luke Schafer
dot syntax is just syntactic sugar, they are the exact same thing, interpretation is run-time in both cases. Property lookup is dynamic whether you use a.b or a['b']
Juan Mendes
+1  A: 

You can't append to it after you call new, because Prototype automatically sends and starts processing the Ajax request upon creation, instead do something like this if you need to alter the parameters object:

var params = {
      "javax.faces.ViewState": encodedViewState,
      "client-id": options._clientId,
      "component-value": options._componentValue
      // Either add your additional properties here as:
      // propertyName : propertyValue
};
// Or add your properties here as:
// params.propertyName = propertyValue;
new Ajax.Request(url, {

method: 'post',

contentType: "application/x-www-form-urlencoded",

parameters: params
});
PatrikAkerstrand
+1  A: 

Assume that the JSON object is named as obj in the Ajax.Request Javascript function. You could now add to the parameters object like this:

obj['parameters']['someproperty'] = 'somevalue';

Hope this helps

ardsrk