admin管理员组

文章数量:1336643

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,
          "ponent-value": options._ponentValue
   }
});

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.

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,
          "ponent-value": options._ponentValue
   }
});

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.

Share Improve this question edited May 26, 2009 at 5:54 staticsan 30.6k5 gold badges63 silver badges73 bronze badges asked May 26, 2009 at 5:51 Joachim SkeieJoachim Skeie
Add a ment  | 

4 Answers 4

Reset to default 4

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,
      "ponent-value": options._ponentValue
}
parameters.foo = 'bar';

var myAjax = new Ajax.Request(url, {
    method: 'post',
    contentType: "application/x-www-form-urlencoded",
    parameters: parameters
});

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

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,
      "ponent-value": options._ponentValue
      // 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
});

Here's a solution I used to append to the JSON object's existing data attribute, but I needed it to be generic enough to handle different key-value pairs. Here's what I created based on this post which seems to work for me.

doAction : function(mData){
    this.data = mData;
    this.appendData = function(mDataToAppend){
        var jsonStrAr = JSON.stringify(mDataToAppend).replace('{','').replace('}','').split('","');
        for(var v = 0; v < jsonStrAr.length; v++){
            var m = jsonStrAr[v].split(':');
            this.data[m[0].replace(/"/g,'')] = m[1].replace(/"/g,'');
        }
    }
}

The result is a single JSON object with n to many attributes, which can then be sent over to the server via the JSON.stringify() mand in the ajax request. Still getting fortable/thinking with JSON, so there might be a better way to do this - in which case, I'm all ears.

本文标签: javascriptAppending to JSON notationStack Overflow