Json is the most frequently used data format in Ajax. Communication between browsers and servers is inseparable from its JSON format description. It can be seen here, which is very detailed and still Chinese.
JSON format description
Note that attribute names in JSON must be enclosed by quotation marks.
Use JSON in jQuery
JQuery is a widely used script library. How can I use JSON in jQuery?
Parse JSON
JQuery provides internal support for JSON parsing,
The jQuery. parseJSON function provides resolution support. For details, see here.
The Code is as follows:
Var obj = jQuery. parseJSON ('{"name": "John "}');
Alert (obj. name = "John ");
Use an object to generate a JSON string
JQuery does not provide a method to directly convert a common JavaScript Object To a json string. You can use the following extension library.
Jquery-json extension Library
This library is used to extend jQuery. For JSON usage, two methods are extended.
The toJSON method is used to serialize a common JavaScript Object into a JSON string.
The Code is as follows:
Var thing = {plugin: 'jquery-json', version: 2.3 };
Var encoded = $. toJSON (thing); // '{"plugin": "jquery-json", "version": 2.3 }'
The evalJSON method parses a JSON string into a common JavaScript Object.
The Code is as follows:
Var thing = {plugin: 'jquery-json', version: 2.3 };
Var encoded = $. toJSON (thing); // '{"plugin": "jquery-json", "version": 2.3 }'
Var name = $. evalJSON (encoded). plugin; // "jquery-json"
Var version = $. evalJSON (encoded). version; // 2.3
This extended: http://code.google.com/p/jquery-json/
Use jQuery with WCF
Client
$. Post in jQuery can directly send a request to the server to parse the data returned by the server in JSON format. However, pay attention to the following points:
The request content type must be in json format, which can be completed through the jQuery-json extension library above. Note that the requested contentType must also be described in text/json, the default post request uses normal name-value pairs. Therefore, the contentType is application/x-www-form-urlencoded. ajaxSetup:
The Code is as follows:
// Ajax settings
$. AjaxSetup ({contentType: 'text/json '});
In this way, the request content type is set to the required type.
Second, the actual request content must be in JSON format, which can be achieved through $. toJSON of the extension library, for example:
$. ToJSON ({x: 2, y: 3 })
In this way, if the server provides a service method Sum, the definition is as follows:
The Code is as follows:
Public int Sum (int x, int y)
{
Return x + y;
}
You can call it as follows. Note that the data returned by WCF is in attribute d.
The Code is as follows:
// Ajax settings
$. AjaxSetup ({contentType: 'text/json '});
$ ("# WcfBtn"). click (function (){
$. Post ("Service1.svc/Sum", $. toJSON ({x: 2, y: 3}), function (data ){
Alert (data. d );
});
});
Server Configuration
First, add the Label [System. ServiceModel. Activation. AspNetCompatibilityRequirements (
RequirementsMode = System. ServiceModel. Activation. AspNetCompatibilityRequirementsMode. Allowed)]
The Code is as follows:
// #1
// Add this label to be used in the script
[System. ServiceModel. Activation. AspNetCompatibilityRequirements (
RequirementsMode = System. ServiceModel. Activation. AspNetCompatibilityRequirementsMode. Allowed)]
// #2
// You also need to set the website configuration file
Public class Service1: IService1
{
Public int Sum (int x, int y)
{
Return x + y;
}
}
Then, in the website configuration file, configure the following.
The Code is as follows:
/>
Example of jQuery using JSON
The Code is as follows:
//// // 1. in HTML, There is a form like this:
//// // Of course, to use the Js function in HTML, you mustJoin