Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending to JSON notation?

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.


2 Answers

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
});
like image 99
Luke Schafer Avatar answered Sep 04 '26 19:09

Luke Schafer


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

like image 25
ardsrk Avatar answered Sep 04 '26 20:09

ardsrk