Learning: jquery uses ajax to retrieve the WebService display table)

Source: Internet
Author: User

In jquery instances, Ajax data retrieval is mostly concentrated on the page for retrieval, but WebService data retrieval is rarely introduced.CodeIs also an example of the nature, the background according to your own situation for more detailed query return.

Here are some notes:

1. the WebService address must be the full name;

2. The data return (datatype) is XML. To return JSON, the code needs to be adjusted;

3. view the example for parameter writing;

Page code:

 

<%... @ Page Language = "C #" autoeventwireup = "true" codefile = "A. aspx. cs" inherits = "A" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT src = "jquery. js"> </SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Table id = "example" border = "1" cellpadding = "4" cellspacing = "0">
<Tr>
<TD> title </TD>
<TD> content </TD>
</Tr>
</Table>
<P>
<Input type = "button" id = "BTN" value = ""/> </P>

<Script language = "JavaScript" type = "text/JavaScript">...
$ (Document). Ready (function ()...{
$ ("# BTN"). Click (function ()...{
Getwebservice ("/myaddress/AA. asmx/getexample ",
... {Title: "test", content: "K! Tomorrow "},
"# Example", ["title", "content"]);
});
})
Function getwebservice (myurl, dataprams, tableid, arrfiledid )...{
$. Ajax (...{
Http://www.cnblogs.com/LeimOO/admin/myUrl,
Data: dataprams,
Datatype: "XML ",
Type: "Post ",
Cache: false,
Success: function (XML )...{
Addtd (XML, tableid, arrfiledid );
},
Error: function (XML, status )...{
Alert (Status );
}
});
}
Function addtd (XML, tableid, arrfiledid )...{
VaR TB = $ (tableid );
// Clear the table and retain the first row
TB. Find ("TR"). Each (function (I )...{
If (I> 0) $ (this). Remove ();
});
// Fill in data
$ (XML). Find ("Table1"). Each (function ()...{
VaR TR = $ ("<tr> </tr> ");
For (var j = 0; j <arrfiledid. length; j ++)
$ ("<TD> </TD>" example .html ($ (this). Find (arrfiledid [J]). Text (). appendto (TR );
Tr. appendto (TB );
})
}
</SCRIPT>
</Form>
</Body>
</Html>

Asmx code:

 

[Webmethod]
Public dataset getexample (String title, string content)
...{
Dataset DS = new dataset ();
Datatable dt = new datatable ();
DT. Columns. Add ("title", type. GetType ("system. String "));
DT. Columns. Add ("content", type. GetType ("system. String "));
Datarow DR = DT. newrow ();
Dr ["title"] = "Party ";
Dr ["content"] = "OK? ";
DT. Rows. Add (DR );
Dr = DT. newrow ();
Dr ["title"] = "Party B ";
Dr ["content"] = "tomorrow! "+ Datetime. Now. tostring (" yyyy-m-d h: M: s ");
DT. Rows. Add (DR );
Dr = DT. newrow ();
Dr ["title"] = title;
Dr ["content"] = content;
DT. Rows. Add (DR );
DS. Tables. Add (DT );
Return Ds;
}

 

ArticleSource: http://www.9yuer.com/thread-2057.html

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.