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 Skeie4 Answers
Reset to default 4you 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
版权声明:本文标题:javascript - Appending to JSON notation? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742239078a2438578.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论