I have the folowing scenario:-
I need to create a Request class (in Javascript) with 开发者_如何转开发the following structure
<Request>
<a/>
<filter>
<x/>
<y/>
<z/>
</filter>
<c/>
<d/>
</Request>
The initial question is, how do I accomplish this in Javascript.
Now, for the broader picture:- This request is an input to a Script Service. So, is this a best practice to create the request object at the client rather than obtain them somehow. If not what is the best practice in doing so
in this blog there is a tutorial how to create Javascript class.
so I suppose you would star by doing something like
var request = {
a: "macintosh",
c: "red",
d: "a",
filter: function (a,b,c) {
// do something
}
}
You've described and XML structure. Is this a required format, or are you open to JSON? I would suggest the later. Also, is this data to be sent with AJAX, or is it to be part of a subsequent POST/GET form submission, etc?
To create a JSON object and its representative string
var request = {
a: 1,
filter: {
x: 4,
y: 5,
z: 6
},
c: 2,
d: 3
};
To convert it to a string, you can use something as simple as Firefox's JSON
object, although I highly suggest that you use a more portable library like Dojo or jQuery (available as a plugin) to accomplish this.
FF:
JSON.stringify(request);
Dojo:
dojo.toJson(request);
jQuery
$.toJSON(request);
精彩评论