簡體   English   中英

從 Java 腳本調用 WebService

[英]Call WebService from Java script

我有簡單的網絡服務:

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

    namespace fmNVBwebSrv
    {
        /// <summary>
        /// Summary description for fm
        /// </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 fm : System.Web.Services.WebService
        {

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

我正在嘗試從 javaScript 調用它:

<head runat="server">
<title>Web Service call from client-side JavaScript</title>
<script language="javascript" type="text/javascript">
function SendRequest() 
{
    fm.HelloWorld(form1.MyTextBox.value, OnComplete, OnError,
    OnTimeOut);
}
function OnComplete(arg)
{
    alert(arg);
}
function OnTimeOut(arg)
{
    alert("timeOut has occured");
}
function OnError(arg)
{
    alert("error has occured: " + arg._message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:55661/fm.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input type="text" value="" id="MyTextBox" />
<input type="button" value="Send Request to the Web Service" 
       id="RequestButton" onclick="return SendRequest()" />
</div>
</form>
</body>

我在控制台出現錯誤Uncaught ReferenceError: fm is not defined 我是 Java Script 新手。 那里缺少什么?

我有同樣的問題並解決了。 調用方法時,需要添加名稱空間:

function SendRequest() 
{
    fmNVBwebSrv.fm.HelloWorld(form1.MyTextBox.value, OnComplete, OnError,
    OnTimeOut);
}

希望這會有所幫助!

您不能像這樣直接從JavaScript調用fm ,因為JavaScript對您的Web服務一無所知,因此您必須告訴JavaScript該怎么做。 檢查此Microsoft頁面以獲取演練 嘗試這個:

<head runat="server">
<title>Web Service call from client-side JavaScript</title>
<script language="javascript" type="text/javascript">
var helloWorldProxy;

// Initializes global and proxy default variables.
function pageLoad()
{
    // Instantiate the service proxy.
    helloWorldProxy = new fmNVBwebSrv.fm();

    // Set the default call back functions.
    helloWorldProxy.set_defaultSucceededCallback(SucceededCallback);
    helloWorldProxy.set_defaultFailedCallback(FailedCallback);
}


// Processes the button click and calls
// the service Greetings method.  
function SendRequest()
{
    var HelloWorld = helloWorldProxy.HelloWorld();
}

// Callback function that
// processes the service return value.
function SucceededCallback(result)
{
    var RsltElem = document.getElementById("Results");
    RsltElem.innerHTML = result;
}

// Callback function invoked when a call to 
// the  service methods fails.
function FailedCallback(error, userContext, methodName) 
{
    if(error !== null) 
    {
        var RsltElem = document.getElementById("Results");

        RsltElem.innerHTML = "An error occurred: " + 
            error.get_message();
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:55661/fm.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input type="text" value="" id="MyTextBox" />
<input type="button" value="Send Request to the Web Service" 
       id="RequestButton" onclick="SendRequest()" />
</div>
</form>
<script language="javascript" type="text/javascript">
pageLoad();
</script>
</body>

在上面的這段代碼中,我在aspx頁面中包含了JavaScript,並在最后調用了pageLoad()函數。 我這樣做只是為了保持您在問題中的處理方式。 但是,最好遵循MSDN示例,方法是將JavaScript文件保存到HelloWorld.js文件中,然后在aspx文件中引用它,如下所示:

<asp:ScriptManager runat="server" ID="scriptManager">
    <Services>
        <asp:ServiceReference path="~/fm.asmx" />
    </Services>
    <Scripts>
        <asp:ScriptReference Path="~/HelloWorld.js" />
    </Scripts>
</asp:ScriptManager>

勾選 web.config 中的 key,設置 mode = "none"。 這解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM