I created a web service named WebService, in which GetTest, SetTest function that obtains set and GUID. Now I want to use this function in javascript in .aspx file. How do I use this function in javascript? I put down the web service code: -
[WebMethod] Public Zero SetTest (Guid ID, String Text) {this.Application.Add (id.ToString (), text); } [WebMethod] Public string GetTest (Guide ID) {Come here. Application [id.ToString ()]. ToString (); } [WebMethod] Public Guarantee CreateNew () {Return Guid.NewGuid (); } [WebMethod] Public string hello () {return "hello world"; }
and more. ASPX code: page code = "c #" AutoEventWireUp = "true" codefile = " % & Gt; & Lt ;; DOCTYPE HTML PUBLIC "- // W3C // DTT XHTML 1.0 Transcription // N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> & Lt; Html xmlns = "http://www.w3.org/1999/xhtml" & gt; & Lt; Head runat = "server" & gt; & Lt; Title & gt; Use of Web Services & lt; / Title & gt; & Lt; Script type = "text / javascript" language = "javascript" & gt; Debugger; Var txtGetTestID = '& lt;% = this.txtGetTest.ClientID% & gt;'; Var txtSetTestID = '& lt;% = this.txtSetTest.ClientID% & gt;'; Var _guid = null; Function GetNew () {//WebService.CreateNew (GetNewDone, OnError, Unknown); GetNewDone (WebService.CreateNew ()); } GetNewDone function (result) {_guid = result; } Function set () {WebService.SetTest (_guid, $ get (txtSetTestID) .value); } GetTest function (//WebService.GetTest(_guid, GetTestDone, OnError, blank); GetTestDone (WebService.GetTest (_guid)); } GetTestDone function (result) {$ get (txtGetTestID) .value = Results; } Function OnError (former) {warning ('error:' pre ._message); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "form1" runat = "server" & gt; & Lt; Div & gt; & Lt; Asp: Label ID = "lblInput" text = "input string" runat = "server" & gt; & Lt; / Asp: Label & gt; & Amp; Nbsp; & Lt; Asp: text box id = "txtInput" runat = "server" & gt; & Lt; / Asp: text box & gt; & Lt; Br / & gt; & Lt; Asp: Label ID = "lblResult" runat = "server" & gt; & Lt; / Asp: label & gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt; ASP: Button ID = "BTN Invoc" Text = "Inquake" Runat = "Server" onclick = "btnInvoke_Click" /> & Lt; / Div & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; TD & gt; & Amp; Nbsp; & Lt; / TD & gt; & Lt; TD & gt; & Lt; ASP: Button ID = "BTN New" Runat = "Server" Text = "New" OnClientClick = "GetNew (); Return Return;" / & Gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; ASP: Textbox ID = "TextTit Test" Runat = "Server" /> & Lt; / TD & gt; & Lt; TD & gt; & Lt; Asp: button id = "btnSetTest" runat = "server" text = "set" OnClientClick = "settest (); return return;" / & Gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; ASP: Textbox ID = "TestGate Test" Runat = "Server" /> & Lt; / TD & gt; & Lt; TD & gt; & Lt; ASP: Button ID = "BTN Gate" runs = "server" text = "Go" Click Click = "Gettast (); Return Back;" / & Gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt; To be able to call javascript from the website, you must first add an explanation of [ScriptMethod].
[System.Web.Script. Services.ScriptService] Public class MyWebService: System.Web.Services.WebService {}
To call webservice, you must include it in a ScriptManager.
& asp: ScriptManager id = "ScriptManager1" runat = "server" & gt; & Lt; Script & gt; & Lt; Asp: ScriptReference path = "~ / script / jquery-1.3.2.js" /> & Lt; Asp: ScriptReference path = "~ / script / jquery-ui-1.7.2.custom.min.js" /> & Lt; Asp: ScriptReference path = "~ / script / json.jquery.js" /> & Lt; / Script & gt; & Lt; Services & gt; & Lt; Asp: ServiceReference path = "~ / WebService.asmx" /> & Lt; / Services & gt; & Lt; / ASP: ScriptManager & gt;
You will now need to call the webservice as follows
[WebServiceNameSpace] MyWebService.MyWebMethod (parameter, function (e) // function for success {}, function (e) // function failure {});
This will be the same for you:
var id = 1; Var text = "bla bla"; Namespace. Web service .settest (id, text, function (e) {}, function (e) {});
You can also use jQuery to call webservice
hh
Comments
Post a Comment