asp.net - Call web service from javascript -


i wrote web service in asp.net, has address:

http://localhost/routegen/service.asmx 

web service has web method getmessage , doesn't take parameters , returns string.

it's right web service, call methods others asp.net apps or android app.

server code:

[webservice(namespace = "http://tempuri.org/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] public class service : system.web.services.webservice {     [webmethod]     public string getmessage() {         return "hello world";     } } 

now need call web method getmessage javascript.

html page: (this web page has no connection web service code, it's totally project! can consider written in win notepad)

... <body id="body1" onload="initialize()" style="behavior:url(webservice.htc)"> </body> ... 

in initialize() method i'm calling:

... service_init(); processresult(); 

and there're functions:

function service_init() {        body1.useservice("http://localhost/routegen/service.asmx?wsdl","theservice");        body1.theservice.callservice("getmessage"); }  function processresult(result) {     alert(result); } 

so relults have:

1)in ie processresult() returns "undefined"

2)in chrome , firefox doesn't work @ (simple alert after useservice doesn't appear)

where problem?how make javascript invoke web method , different browsers?

in aspx section,

add scriptmanager tag follows,

        <asp:scriptmanager id="scriptmanager1" runat="server">                 <services>                    <asp:servicereference  path="~/sample.asmx"/>                 </services>          </asp:scriptmanager> 

in javascript call web service(sample.asmx) follows,

<script language="javascript" type="text/javascript">    function calledonanyclientclickevent()   {      var parameter1="dsfsfs";        namespace1.webservice1.helloworld(parameter1,onsucess,onfail);   }    function onsuccess(asd)    {       alert(asd);//result contain return parameter web service    }     function onfail(asd)    {       alert(asd);    } </script> 

see asmx section (sample.asmx) below,

          using system;           using system.collections.generic;           using system.linq;           using system.web;           using system.web.services;            using system.web.script.serialization;           using system.web.script.services;            namespace namespace1           {             /// <summary>             /// summary description webservice1             /// </summary>            [webservice(namespace = "http://tempuri.org/")]            [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]                  [system.componentmodel.toolboxitem(false)]             // allow web service called script, using asp.net ajax, uncomment following line.             [scriptservice]             public class webservice1 : system.web.services.webservice            {                    [webmethod]                    public string helloworld(string par1)                    {                         //do logic here                         return "hello world";                    }             }          } 

hope helps...


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -