4

I wrote web service in ASP.NET, it has this address:

http://localhost/RouteGen/Service.asmx

Web Service has web method GetMessage , it doesn't take any parameters and returns a string.

It's all right with web service, I call its methods from others ASP.NET apps or even from 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 I need to call web method GetMessage from javascript.

html page: (this web page has no connection with web service code, it's totally another project! You can consider that it is 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 this functions:

function service_init()
{   
    body1.useService("http://localhost/RouteGen/Service.asmx?WSDL","TheService");   
    body1.TheService.callService("GetMessage");
}

function processResult(result)
{
    alert(result);
}

So relults I have:

1)In IE processResult() returns "undefined"

2)In Chrome and FireFox it doesn't work at all (simple alert after useService doesn't appear)

Where is the problem?How to make javascript invoke web method normally and from different browsers?

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84
  • I don't know why some body down voted it with out any comment. Any way my answer working perfectly... – Harun Jul 02 '11 at 19:21

2 Answers2

5

In Aspx section,

Add a ScriptManager tag as follows,

        <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                   <asp:ServiceReference  path="~/sample.asmx"/>
                </Services>
         </asp:ScriptManager>

In JavaScript call the web service(sample.asmx) as follows,

<script language="javascript" type="text/javascript">

  function CalledOnAnyClientClickEvent()
  {
     var parameter1="dsfsfs"; 

     NameSpace1.WebService1.HelloWorld(parameter1,OnSucess,OnFail);
  }
   function OnSuccess(asd)
   {
      alert(asd);//result will contain the return parameter from web service
   }

   function OnFail(asd)
   {
      alert(asd);
   }
</script>

See the 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 for WebService1
            /// </summary>
           [WebService(Namespace = "http://tempuri.org/")]
           [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]     
            [System.ComponentModel.ToolboxItem(false)]

           // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
           [ScriptService]

           public class WebService1 : System.Web.Services.WebService
           {

                  [WebMethod]
                   public string HelloWorld(string par1)
                   {
                        //do your logic here

                       return "Hello World";
                   }
            }
         }

Hope this helps...

Harun
  • 5,109
  • 4
  • 38
  • 60
  • `NameSpace1.WebService1.HelloWorld(parameter1,OnSucess,OnFail);` is instead of `useService` and `callService` methods? And how can I use Namespace1 in javascript code?It's another html page that has no connection with web service code – Ilya Blokh Jul 02 '11 at 11:39
  • Hey, This is why a script manager with the path of web service is specified. I've implemented this and it worked for me... – Harun Jul 02 '11 at 18:02
  • @Ilya Blokh, I've tried it once again and this worked perfectly. I don't know why some body down voted it with out any comment. – Harun Jul 02 '11 at 19:10
  • Where did you write javascript code?It should be on html page that was created after asp.net server, not with.You already have web service with web method, I need write something in javascript to call this method – Ilya Blokh Jul 06 '11 at 07:02
  • why `NameSpace1.WebService1.HelloWorld(parameter1,OnSucess,OnFail);` works?where in web page you define NameSpace1?? – Ilya Blokh Jul 06 '11 at 07:07
  • @Ilya Blokh, For accessing the webservice from client side the path of the webservice is specified inside the scriptmanager tag placed in the aspx section (refer Add a ScriptManager tag in my answer). As a result you do not need to define NameSpace1(which is the namespace of the webservice- Refer the See the Asmx section of my answer). Also the javaScript section in visualStudio won't show intellisense for these. Just type them and check whether this is working by placing a break point in the webservice WebMethod. – Harun Jul 06 '11 at 09:12
2

ASMX is a SOAP web service. SOAP is relativily complicated.

A better way to return data to the browser is to use REST. REST Services can be consumed using JQUERY.

You can build a WCF Services that uses REST and returns a JSON result.

If your service is not on the same server as your web page, you will have to use something like JSONP to do a cross domain call.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • WCF is great, especially for authenticated web services and critical data. For a basic CRUD web service, I've built it in ASP.NET MVC and it works like a champ. – Chase Florell Jul 02 '11 at 19:18
  • @rockinthesixstring, could you please say what is wrong with my answer? It is working perfectly for me... – Harun Jul 02 '11 at 19:23
  • @Harun, I didn't down vote your answer, nor did I look at it. I stopped using web forms all most all together, so I'm not a fan anything that includes `runat="server"`. I'm not at all saying it's wrong, I just didn't look at it, or vote on it. – Chase Florell Jul 02 '11 at 20:15
  • And what about calling web methods from javascript? – Ilya Blokh Jul 06 '11 at 07:09