Ajax call of xmlwebservice in jquery

Source: Internet
Author: User
Step 1: Return XML strings from the database Step 1: Return XML strings from the database # Region Step 1: Return XML strings from the database

Public   Static   String Getxml ( Int Parentid)
{
Ilist < Categoryinfo > Listctgs =   New Category (). getcategories (parentid );
String XML =   " <Ctgs> " ;
For ( Int I =   0 ; I < Listctgs. Count; I ++ )
{
XML + =   " <CTG> " ;
XML + =   " <Name> "   + Replacexml (listctgs [I]. categoryname) +   " </Name> " ;
XML + =   " <NO> "   + Listctgs [I]. categoryno +   " </NO> " ;
XML + =   " <Haschild> "   + Listctgs [I]. haschild. tostring () +   " </Haschild> " ;
XML + =   " <Namepath> "   + Replacexml (listctgs [I]. namepath) +   " </Namepath> " ;
XML + =   " </CTG> " ;
}
XML + =   " </Ctgs> " ;
Return XML;
}

# Endregion

Step 2: Create a new category. asmx and return the xmlwebmethod XML document. Step 2: Create a new category. asmx and return the xmlwebmethod XML document. # Region Step 2: Create a new category. asmx and return the xmlwebmethod XML document.

[Webmethod]
Public Xmldatadocument getcategoriesxml ( Int Categoryno)
{
Xmldatadocument XD= NewXmldatadocument ();
XD. loadxml (getxml (categoryno ));
ReturnXD;
}

# Endregion

Step 3 Write a common jquery Ajax return XML method with a callback function Step 3 Write a common jquery Ajax return XML method with a callback function # Region Step 3 Write a common jquery Ajax return XML method with a callback function

Function xmlwebservice (URL, pars, callback) {
$. Ajax ( {
Data: pars,
URL: URL,
Type: " Post " ,
Contenttype: " Text/XML; UTF-8 " , // UTF-8 is required.
Datatype: ' XML ' ,
Cache: True ,
Success: function (data) {
Callback (data );
} ,
Error: function (data, status) {
Alert ('Ajax xmlwebservicer error \ n'+Data. responsetext );
}
}
);
}

# Endregion

Step 4: Read the nodes in XML to the control on the Web Front-end. Step 4: Read the nodes in XML to the control on the Web Front-end. # Region Step 4: Read the nodes in XML to the control on the Web Front-end.

$ (Document). Ready (function () {
Initcurpage ();
} );

// Initial page
Function initcurpage ()
{
Readlevelctgs (1,1);
}

// List of subclass XML format
Function readlevelctgs (levelnum, parentid)
{
// Jquery Ajax value and callback
Xmlwebservice ( ' Category. asmx/getcategoriesxml ' , {Categoryno: parentid} , Function (data) {Buildlevelctgs (levelnum, data )} );
}  

// Bind the read XML file to the front-end control.
Function buildlevelctgs (levelnum, XML)
{
$ (Curdivid). Text ( "" );
$ (XML). Find ( ' CTG ' ). Each (function () {
VaR ctgdiv=$ ('<Div> </div>');
Ctgdiv. appendto (curdivid );
} );
}

# Endregion

// Author: Keen
// Note: jquery AJAX can return

From: http://www.cnblogs.com/xiaobaigang/archive/2008/04/03/1135984.html

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.