Respond and reply with JSON in server-side scripts and programs
Introduction: In an article recently published in this series, you've learned how to convert JavaScript objects to JSON format. This format is easily used to send (and receive) data corresponding to an object or even an array of objects. In the last article in this series, you will learn how to handle data sent to the server in JSON format and how to reply to the script in the same format.
The true value of JSON
As described in the previous article in this series, JSON is a valid format for AJAX applications because it enables fast conversion between JavaScript objects and string values. Because Ajax applications are ideal for sending plain text to server-side programs and receiving plain text, APIs that generate text are naturally preferable to APIs that do not generate text, and JSON allows you to work with local JavaScript objects without having to worry about how to represent those objects.
XML can also provide similar benefits in terms of text, but several existing APIs for translating JavaScript objects into XML do not mature with the JSON API; Sometimes you have to be extra cautious when creating and working with JavaScript objects to ensure that the processing you do can work with the selected XML Session API collaboration. But for JSON, the situation is quite different: it handles almost every possible object type and returns a very good JSON data representation.
As a result, the greatest value of JSON is that JavaScript can really be processed as JavaScript rather than as a data format language. All the tricks you've learned about using JavaScript objects can be applied to your code without having to worry about how to turn those objects into text. After that, you can make a simple JSON method call as follows:
String myObjectInJSON = myObject.toJSONString();
You can now send the results Wenbenfal to the server.
Send JSON to Server
Sending JSON to a server is not difficult, but it is critical, and there are some important choices to make. However, once you decide to use JSON, these choices are simple and limited in number, so you need to consider and focus on a few things. It is important to be able to send the JSON string to the server, and it is best to do it as quickly and as simple as possible.
Send JSON with name/value pairs via get
The easiest way to send JSON data to a server is to convert it to text and then send it as the value of the name/value pair. It is important to note that the JSON-formatted data is a fairly long object and may look like the one shown in Listing 1:
Listing 1. Simple JavaScript object in JSON format
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin",
"email": "brett@newInstance.com" }, { "firstName": "Jason", "lastName":"Hunter",
"email": "jason@servlets.com" }, { "firstName": "Elliotte", "lastName":"Harold",
"email": "elharo@macfaq.com" } ], "authors": [ { "firstName": "Isaac",
"lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad",
"lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank",
"lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] }