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