I'm trying to teach myself AJAX/Web services using C# and javascript. And I think I'm having a namespace issue, judging by the Google-ing I've done.
First of all, my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace SimpleAJAX
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string GetServerResponse(string callerName)
{
if (callerName == string.Empty)
throw new Exception("Web Service Exception: invalid argument");
return string.Format("Service responded to {0} at {1}", callerName, DateTime.Now.ToString());
}
}
}
The Web Service.
<head id="Head1" runat="server">
<title>Web Service call from client-side JavaScript</title>
<script language="javascript" type="text/javascript">
function SendRequest() {
MySampleService.GetServerResponse(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="~/WebServices/MySampleService.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>
The aspx page.
Taken from ( http://www.semenoff.dk/en/Code-Corner/ASP.Net.AJAX/WebService-From-JavaScript.aspx )
And when I run it I get a "'MySampleService' is undefined" error. I followed the tutorial exactly, but obviously I'm still doing something wrong. Little help? Thanks!
You'll need to include your namespace and your service's actual class name in the call:
SimpleAJAX.WebService1.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);
To see the exact structure of the JavaScript proxy that ASP.NET generates for your ScriptReference, open WebService1.asmx/jsdebug
in a browser.
try this
function SendRequest() {
SimpleAJAX.WebService1.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);
}
You don't have any class called "MySampleService". Your class is named WebService1, so you should use
SimpleAJAX.WebService1.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError, OnTimeOut);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.