Use JSON implementation code in jQuery _ jquery

Source: Internet
Author: User
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

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.