简体   繁体   English

如何调用WebService客户端?

[英]How to call WebService client-side?

I am trying to access webservice from client-side using javascript. 我正在尝试使用javascript从客户端访问webservice。 I have created webservice successfully. 我已经成功创建了webservice。 I have used it in Website project successfully, but it is Web Application project. 我已成功在网站项目中使用它,但它是Web应用程序项目。

Here is my HTML code 这是我的HTML代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">
        function Test() {
            WebService1.HelloWorld(onS, onF);
        }
        function onS(r) { alert("Result"); }
        function onF(er) { alert("Error"); }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference InlineScript="true" Path="~/WebService1.asmx"/>
            </Services>
        </asp:ScriptManager>
        <input type="button" value="Call Service" onclick="Test()" />
    </div>
    </form>
</body>
</html>

Here is WebService code 这是WebService代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestWebService
{
    /// <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. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

Now this code is not working. 现在这段代码不起作用了。

I am using framework 3.5 and VS2012. 我正在使用框架3.5和VS2012。

I find the answer. 我找到了答案。 I should use namespace before web service name like this 我应该在web服务名之前使用命名空间

function Test() {
    TestWebService.WebService1.HelloWorld(onS, onF);
}

This is the difference between Website project and Web Application project. 这是Website项目和Web应用程序项目之间的区别。

You can use the AJAX capability of JQuery to hit the web service. 您可以使用JQuery的AJAX功能来访问Web服务。 It is easy to use and very powerful. 它易于使用且功能强大。

Similar questions have been answered on stackoverflow.com before. 之前在stackoverflow.com上已经回答了类似的问题。 Here is a link for you to check out. 这是一个供您查看的链接。

jquery ajax get example jquery ajax得到例子

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM