Jquery Ajax instance 6: send a request to WebService and return the DataSet (XML) asynchronous call

Source: Internet
Author: User

1. WebService. asmx:

Process business data and generate DataSet (XML) data in the GetDataSet () method for JqueryRequest. aspx to call. The Code is as follows:

[WebMethod]
Public DataSet GetDataSet ()
{
DataSet ds = new DataSet ();
DataTable dt = new DataTable ();
Dt. Columns. Add ("Name", Type. GetType ("System. String "));
Dt. Columns. Add ("Password", Type. GetType ("System. String "));
DataRow dr = dt. NewRow ();
Dr ["Name"] = "Xiaohua ";
Dr ["Password"] = "aaaaaaaaa ";
Dt. Rows. Add (dr );
Dr = dt. NewRow ();
Dr ["Name"] = "Xiao Bing ";
Dr ["Password"] = "bbbbbbbbbbb ";
Dt. Rows. Add (dr );
Ds. Tables. Add (dt );
Return ds;
}

Ii. AjaxRequest. aspx

Click the button to request the GetDataSet () method of WebService. asmx to obtain the XML data object. The Code is as follows:

// Return DataSet (XML)
$ (Document). ready (function (){
$ ('# BtnDataset'). click (function (){
$. Ajax ({
Type: "POST ",
Url: "WebService. asmx/GetDataSet ",
Data :"{}",
DataType: 'xml', // The returned type is xml
Success: function (result) {// method executed when the call succeeds
// Capture and output exceptions during processing
Try {
$ (Result). find ("Table1"). each (function (){
$ ('# Dd '). append ($ (this ). find ("Name "). text () + "" + $ (this ). find ("Password "). text ());
});
}
Catch (e ){
Alert (e );
Return;
}
},
Error: function (result, status) {// when an error occurs, the callback function is executed.
If (status = 'error '){
Alert (status );
}
}
});
});
});

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.