C # The basic Ajax writing method is now using jquery's Ajax function, but do not forget the basic writing method.
<SCRIPT type ="Text/JavaScript">
$ (Function()
{
VaRXhr =NewAjaxxmlhttprequest ();
$ ("# Btnajaxold"). Click (Function(Event)
{
VaRXhr =NewAjaxxmlhttprequest ();
// XMLHTTP is not a synchronous request by default (also recommended), that is, the open method does not return the data returned by the server as the downloadstring of WebClient. It is asynchronous, therefore, you must listen to the onreadystatechange event.
Xhr. onreadystatechange =Function()
{
If(Xhr. readystate = 4) // indicates that the server is complete.
{
If (xhr. Status = 200) // If the status code is 200, it is successful.
{
Document. getelementbyid ("Divresult"). Innerhtml = xhr. responsetext;
} Else {
Alert ("error returned by Ajax server! ")
}
}
}
Xhr. Open ( "Get" , "Data/ajaxgetcityinfo. aspx? Resulttype = html" , True );
Xhr. Send ( Null );
});
})
// Cross-browser XMLHTTPRequest object
Function Ajaxxmlhttprequest ()
{
VaR XMLHTTP;
Try
{
// Firefox, opera 8.0 +, Safari
XMLHTTP = New XMLHttpRequest ();
}
Catch (E)
{
// Internet Explorer
try
{< BR> XMLHTTP = New activexobject ( "msxml2.xmlhttp" );
}< br> catch (e)
{
Try
{
XMLHTTP = New Activexobject ( "Microsoft. XMLHTTP" );
}
Catch (E)
{
Alert ( "Your browser does not support Ajax! " );
Return False ;
}
}
}
Return XMLHTTP;
}
</ Script >
Jquery Ajax
List page
<Script language = "JavaScript" type = "text/JavaScript">
Function initlist (){
$ ("# Liststr" pai.html ("");
VaR strhtml = "";
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "ws_page.asmx/initlist ",
Data: "{ID: '" + $ ("# txtid "). val () + "', URL:'" + $ ("# txturl "). val () + "', IP:'" + $ ("# txtip "). val () + "'}",
Datatype: 'json ',
Cache: false,
Success: function (JSON ){
VaR objlist = eval (JSON. D );
$. Each (objlist, function (n, OBJ ){
Strhtml + = "<tr> ";
Strhtml + = "<TD> ";
Strhtml + = obj. ID;
Strhtml + = "</TD> ";
Strhtml + = "<TD> ";
Strhtml + = obj. url;
Strhtml + = "</TD ";
Strhtml + = "<TD> ";
Strhtml + = obj. IP;
Strhtml + = "</TD> ";
Strhtml + = "<TD> ";
Strhtml + = obj. updatetime;
Strhtml + = "</TD> ";
Strhtml + = "<TD> ";
Strhtml + = "<input type = 'button 'value = 'edit 'onclick = 'javascript: location. href = \" manager. aspx? Id = "+ obj. ID +" \ "'/> & nbsp ;";
Strhtml + = "<input type = 'button 'value = 'delete' onclick = 'deleteinfo (" + obj. ID + ") '/> ";
Strhtml + = "</TD> ";
Strhtml + = "</tr> ";
})
$ ("# Liststr"). append (strhtml );
},
Error: function (ERR ){
Alert (ERR. responsetext );
}
});
}
Function deleteinfo (ID ){
If (! Confirm ('Are you sure you want to delete it? ') Return;
If (isnan (ID )){
Alert ('parameter error ');
Return;
}
$. Ajax ({
Type: 'post ',
Contenttype: 'application/json ',
URL: 'ws _ page. asmx/deleteinfo ',
Data: "{ID:" + ID + "}",
Datatype: 'json ',
Cache: false,
Success: function (JSON ){
If (JSON. d = "success "){
Alert ('deleted successfully! ');
Initlist ();
} Else {
Alert (JSON. D );
}
},
Error: function (ERR ){
Alert (ERR. responsetext );
}
});
}
$ (Document). Ready (function (){
Initlist ();
});
</SCRIPT>
// Manager page
<Script language = "JavaScript" type = "text/JavaScript">
VaR id = $. query. Get ('id'); // You must reference the urlparameter. js plug-in to obtain the passed parameters.
Function getinfobyid (){
If (ID! = ""&&! Isnan (ID )){
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "ws_page.asmx/getinfobyid ",
Data: "{ID:" + ID + "}",
Datatype: 'json ',
Cache: false,
Success: function (JSON ){
VaR DATA = eval (JSON. D );
$ ("# Txtip"). Val (data [0]. IP );
$ ("# Txturl"). Val (data [0]. url );
},
Error: function (ERR ){
Alert (ERR. responsetext );
}
});
}
}
Function submitevent (){
If (ID = "" | isnan (ID )){
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "ws_page.asmx/addinfo ",
Data: "{URL: '" + $ ("# txturl "). val () + "', IP:'" + $ ("# txtip "). val () + "'}",
Datatype: 'json ',
Cache: false,
Success: function (JSON ){
If (JSON. D. indexof ("success") = 0 ){
Id = JSON. D. substring (7 );
Alert ("added successfully! ");
}
Else {
Alert (JSON. D );
}
},
Error: function (ERR ){
Alert (ERR. responsetext );
}
});
} Else {
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "ws_page.asmx/updateinfo ",
Data: "{URL: '" + $ ("# txturl "). val () + "', IP:'" + $ ("# txtip "). val () + "', ID:" + ID + "}",
Datatype: 'json ',
Cache: false,
Success: function (JSON ){
If (JSON. d = "success "){
Alert ("modified successfully! ");
} Else {
Alert (JSON. D );
}
},
Error: function (ERR ){
Alert (ERR. responsetext );
}
});
}
}
$ (Document). Ready (function (){
Getinfobyid ();
});
</SCRIPT>