Server-to-client data interaction (JSON)

Source: Internet
Author: User

The server returns to the client JSON object, what type it is, and what it becomes after Ajax processing.> Guess 1: The data returned by the server with double quotes: "Return value", Reason: Because the returned type is a string. result Matchless quotation marks: {"ID": 1, "Name": "Hpy", "Age" :error, the returned data is indeed a string type. but double quotes are just the syntax of strings in C # (strings are enclosed in double quotes)when the output to the Web page (HTML) is displayed as is. dataType: ' text '/' HTML 'string{"ID": 1, "Name": "Hpy", "Age" :> Guess 2: If it is processed by Ajax and becomes the specified type, then AJAX handles the string. That 's right. but the output string, also does not display double quotation marks, because the double quotation marks are only as a representation of the string in the JS language. therefore, the output string is not enclosed with double quotes. To determine the type of data, you need to use the TypeOf syntax in JS. Summary:1, strings in the program have their own representation (single quotation marks, double quotation marks), and the output string is basically the same as the output. so we need to know the type of the data and we need to use the TypeOf method in the program. 2, the server is returned to the client is a string, if the client wants to get an object type, you need a ruleto store the string. The JSON syntax appears in JS to define the object format. The server serializes the data into JSON format characters back to the client, at which point the clientthe string is then converted to an object. jquery can automatically convert us to JSON format by setting Ajax parameters, DataType: ' JSON '. and we want to transform ourselves into 3 different forms. >> What is json> json? JSON (JavaScript Object Notation) is a lightweight data interchange format. JSON Syntax rulesthe JSON syntax is a subset of the JavaScript object representation syntax. data in name/value pairsdata is separated by commascurly Braces Save Objectsquare brackets Save ArrayJSON valuethe JSON value can be:number (integer or floating point)string (in double quotes)logical value (TRUE or false)Array (in square brackets)object (in curly braces)NULL> Several ways to convert a string type to a JSON type.1. Evalvar jsonobj = eval (' (' +str+ ') '); 2. New functionjsonobj = (new Function ("return" + str));3, Json.parse (str);jsonobj = Json.parse (str);//STR must be in strict JSON format and attributes need to be enclosed in strings. //Otherwise conversion failed//And Json.parse is only available in IE8 or later. > Conversion ErrorsThe conversion fails when the value of the string does not conform to the JSON format.

Server-to-client data interaction (JSON)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.