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?

1
  • I don't know why some body down voted it with out any comment. Any way my answer working perfectly... Commented Jul 2, 2011 at 19:21

2 Answers 2

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...

Sign up to request clarification or add additional context in comments.

6 Comments

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
Hey, This is why a script manager with the path of web service is specified. I've implemented this and it worked for me...
@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.
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
why NameSpace1.WebService1.HelloWorld(parameter1,OnSucess,OnFail); works?where in web page you define NameSpace1??
|
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.

4 Comments

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.
@rockinthesixstring, could you please say what is wrong with my answer? It is working perfectly for me...
@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.
And what about calling web methods from javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.