Jquery + ajax call webservice instance this article uses asp.net's webservice and jquery's ajax to create an ajax instance
Jquery + ajax call webservice instance
This article uses the asp tutorial. net webservice and jquery ajax to create an ajax instance.
<Script language = "webpage effect">
$ ("Document"). ready (function (){
$ ("# Mmlog"). append ("data"); // This statement is valid. data is found at the specified position on the page.
$ ("# Btn"). click (function (){
Alert ("sdfsdf"); // valid
$. Ajax ({
Async: false,
Type: "post ",
Contenttype: "application/json ",
Url: "webservice. asmx/helloworld ",
Data :"{}",
Datatype: "xml ",
Cache: false,
Success: function (data, textstatus, xmlhttprequest ){
Alert ("OK"); // type: "get", at least once in 10 times is invalid; type: "post", invalid
$ ("# Mmlog"). append ("data"); // It disappears after being displayed.
Alert (data );
},
Error: function (xml, status) {alert (status) ;}// type: "post", status = "error"
});
});
})
</Script>
Webservic. asmx File
Public class dataservice: system. web. services. webservice {
Public dataservice (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod]
Public string helloworld (){
Return "hello world ";
}
[Webmethod]
Public int appenddata (string sdev, string x, string y)
{
String s = string. format ("insert into zry_current (zdev, zx, zy) values ('{0}', {1}, {2})", sdev, x, y );
Return accesshelper.exe cutenonquery (s );
}
[Webmethod]
Public string readdata (string sdev, string dayid)
{
String s = string. format ("select zx, zy from zry_current where zdev = '{0}' and zday = {1}", sdev, dayid );
Dataset ds = accesshelper.exe cutedataset (s );
Ds. datasetname = "zry_current ";
Return ds. getxml ();
}
}