Previously encountered problems were not implemented using the ajaxpro method. Later, the jquery. Ajax method was used to call the datatable returned by the webmethod in the background. If you have any questions about converting a able into a JSON format, I will write an example to describe the implementation process.
1. Create testajax2.aspx;
<Body>
<Form ID = "form1" runat = "server">
<Input id = "btnjson" type = "button" value = "JSON"/>
<Div class = "case_input1" id = "article">
<Ul>
</Ul>
</Div>
</Form>
</Body>
2. Add reference system. Web. extension.
Add namespace
Using system. Web. Services;
Using system. collections;
Using system. Web. Script. serialization;
3. construct the webmethod method in the. CS file and return the datatable type.
[Webmethod]
Public static string getdatatable (string CID, string site)
{
Dataset DS = new usercase (). getresult (CID, site, String. Empty );
String S = dttojson (Ds. Tables [0]);
Return S. tostring ();
}
4. Add a method to convert a able to JSON.
Public static string dttojson (datatable DT)
{
Javascriptserializer JSS = new javascriptserializer ();
Arraylist DIC = new arraylist ();
Foreach (datarow row in DT. Rows)
{
Dictionary <string, Object> Drow = new dictionary <string, Object> ();
Foreach (datacolumn Col in DT. columns)
{
Drow. Add (Col. columnname, row [col. columnname]);
}
Dic. Add (Drow );
}
Return JSS. serialize (DIC );
}
5. Use jquery to call Ajax on the aspx page.
<SCRIPT type = "text/JavaScript" src = "jquery-1.6.2.js"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
$ (Function (){
$ ("# Btnjson"). Click (function (){
$. Ajax ({
Type: "Post ",
URL :" Testajax2.aspx/getrelatedarticle ", // URL page/method name
Data: "{'cid': 'c0503', 'SITE': 'tp '}", // parameter {key/value}
Contenttype: "application/JSON; charset = UTF-8 ",
Datatype: "JSON ",
Success: function (data ){
Data = jquery. Parsejson (Data. D );
$. Each (data, function (I, item ){
$ ("# Article ul "). append ('<li> <a href = "' + item. href + '"target = _ blank>' + item. title + '</a> </LI> ')
})
},
Error: function (error ){
Alert (error );
}
});
})
})
</SCRIPT>