我有简单的网络服务:
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的新手。那里缺少什么?
答案 0 :(得分:1)
我有同样的问题并解决了它。调用方法时需要添加命名空间:
function SendRequest()
{
fmNVBwebSrv.fm.HelloWorld(form1.MyTextBox.value, OnComplete, OnError,
OnTimeOut);
}
希望这会有所帮助!
答案 1 :(得分:0)
您无法直接从JavaScript中调用fm
,因为JavaScript对您的网络服务一无所知,因此您必须告诉JavaScript该做什么。检查此Microsoft page for a walkthrough。试试这个:
<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()
函数。我这样做只是为了保持你在问题中的方式。但是,最好通过将JavaScript文件保存到HelloWorld.js
文件中然后在您的aspx文件中引用它来遵循MSDN示例,如下所示:
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference path="~/fm.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="~/HelloWorld.js" />
</Scripts>
</asp:ScriptManager>
答案 2 :(得分:0)
检查web.config中的key,设置mode = "none"。这解决了我的问题。