admin管理员组

文章数量:1277899

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 = "/")]
[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?

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/")]
[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?

Share Improve this question edited Jul 6, 2011 at 6:55 Eli Blokh asked Jul 2, 2011 at 10:04 Eli BlokhEli Blokh 12.3k13 gold badges54 silver badges86 bronze badges 1
  • I don't know why some body down voted it with out any ment. Any way my answer working perfectly... – Harun Commented Jul 2, 2011 at 19:21
Add a ment  | 

2 Answers 2

Reset to default 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/")]
           [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]     
            [System.ComponentModel.ToolboxItem(false)]

           // To allow this Web Service to be called from script, using ASP.NET AJAX, unment 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...

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

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.

本文标签: aspnetCall web service from javascriptStack Overflow