Jquery implements Ajax and returns JSON data

Source: Internet
Author: User

Jquery can call several methods to implement Ajax

I often use the $ get (URL, Data, callback, type) method.

The URL is the page of the asynchronous request (which can be. ashx file), data is the parameter, callback is the callback function, and type is the type of the returned data. types include XML, HTML, JSON, and text.

First, the page references jquery. js

Write JS functions processed by Ajax on the page

 

Function initmeeting () {$. get ("/common/meetingroom. ashx ", {meetid: <% = meetid % >}, function sellerlist (data) {$ (" # divsellerlist ").html (data) ;}," JSON "); setTimeout ("initmeeting ()", 20000);} function initmeeting () {$. get ("/common/meetingroom. ashx ", {meetid: <% = meetid % >}, function sellerlist (data) {var OBJ = eval (" ("+ Data + ")"); // converted JSON object $ ("# divsellerlist" ).html (obj. cellerlist) ;}, "html"); setTimeout ("initmeeting ()", 20000 );}

The returned type is JSON, which can return data types of similar classes. For example, {"name": "Sunny D. D", "Age": 25}

However, when using the returned data value, you must first convert the JSON

 

VaR OBJ = eval ("(" + Data + ")"); // converted JSON object

You can get the JSON object.

 

The JSON object is generated in the meetingroom. ashx file.

 

Some code is as follows:

 

Public class meetingroom: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; try {int meetid = xyconvert. getint32 (context. request. querystring ["meetid"]); string STR = ""; meetingjson = new meetingjson (); If (meetid! = 0) {meetingjson. cellerlist = returncellerlist (meetid);} STR = jsonconvert. serializeobject (meetingjson); context. response. write (STR);} catch (exception ex) {context. response. write (ex. message) ;}} public class meetingjson {string cellerlist; Public String cellerlist {get {return cellerlist;} set {cellerlist = value ;}} protected string returncellerlist (INT meetid) {// omitted ..}}

The most important thing to return the JSON data format is to use the jsonconvert. serializeobject () method. It can package the data to be passed to the client and serialize it as a string.

The class jsonconvert can be used by introducing DLL in a third-party DLL file (newtonsoft. JSON. dll.

 

Note: In. in the ashx page, if you want to use the session, write the context directly. session ["user"] does not work. You must specify the current context to use the session. You can use the irequiressessionstate interface to access the session.

 

    public class MeetingRoom : IHttpHandler, IRequiresSessionState     {

 

In small and medium-sized projects, Ajax is implemented in this way. For large projects, there should be a better encapsulated Ajax framework.

 

 

 

 

 

Related Article

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.