开发者

Call web service from javascript

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 f开发者_Go百科rom 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜