从 javascript 调用 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6556334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:05:11  来源:igfitidea点击:

Call web service from javascript

javascriptasp.netweb-servicesbrowser

提问by Ilya Blokh

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

我在 ASP.NET 中编写了 Web 服务,它有这个地址:

http://localhost/RouteGen/Service.asmx

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

Web Service 具有 Web 方法GetMessage,它不带任何参数并返回一个字符串。

It's all right with web service, I call its methods from others ASP.NET apps or even from Android app.

Web 服务没问题,我从其他 ASP.NET 应用程序甚至 Android 应用程序调用它的方法。

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

现在我需要GetMessage从 javascript调用 web 方法。

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)

html页面:(这个网页和web服务代码没有关系,完全是另外一个项目!你可以考虑用win记事本写的)

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

in initialize() method I'm calling:

在我调用的 initialize() 方法中:

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

1)在IE中processResult()返回“未定义”

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

2)在 Chrome 和 FireFox 中它根本不起作用(useService 后没有出现简单的警报)

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

问题出在哪里? 如何让javascript从不同浏览器正常调用web方法?

回答by Harun

In Aspx section,

在 Aspx 部分,

Add a ScriptManager tag as follows,

添加一个 ScriptManager 标签,如下所示,

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

在 JavaScript 中调用 web 服务(sample.asmx)如下,

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

请参阅下面的 Asmx 部分 (sample.asmx),

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

希望这可以帮助...

回答by Shiraz Bhaiji

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

ASMX 是一种 SOAP 网络服务。SOAP 相对复杂。

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

将数据返回到浏览器的更好方法是使用 REST。可以使用 JQUERY 使用 REST 服务。

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

您可以构建使用 REST 并返回 JSON 结果的 WCF 服务。

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.

如果您的服务与您的网页不在同一台服务器上,您将不得不使用 JSONP 之类的东西来进行跨域调用。