Asmx webservice referencing issue
I need to call my web service method with JavaScript in my client page. I think I am not properly referencing this correctly and would appreciate help figuring this out please.
The error message says "CalendarHandler is not defined".
<%@ WebService Language="C#" CodeBehind="~/App_Code/CalendarHandler.cs"
Class="CalendarHandler" %>
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeFile="CalendarPage.aspx.cs" Inherits="CalendarPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<input type="button" id="loadevents" onclick="callLoadEvents();" />
<div id="eventresults"> </div>
<div id="resultFailed"> </div>
<script language="javascript" type="text/javascript">
var tasks;
function callLoadEvents() {
Speak.CalendarHandler.GetEvents(GetLoadAddress_success, OnFailed);
}
function GetLoadAddress_success(e) {
tasks = e;
}
// --------------------------
function OnFailed() {
$get('resultFailed').innerHTML = "failed";
}
</script>
</asp:Content>
using System.Web;
using System.Web.Services;
[WebService(Namespace = "Speak")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class CalendarHandler : System.Web.Services.WebService
{
static IDictionary<DateTime, String> Calendarevents;//hold my events in this
public CalendarHandler 开发者_如何学JAVA() {
Calendarevents = new Dictionary<DateTime, string>();
Calendarevents.Add(DateTime.Now, "Going to meeting with XYZ Company");
Calendarevents.Add(DateTime.Now.AddDays(1), "XML Class at 2pm");
Calendarevents.Add(DateTime.Now.AddDays(1),"ASP.NET 3.5 Ajax");
Calendarevents.Add(DateTime.Now.AddDays(1),"Pocket Guide");
Calendarevents.Add(DateTime.Now.AddDays(1),"Grocery Shopping");
}
[WebMethod]
public IDictionary<DateTime, String> GetEvents()
{
return Calendarevents;
}
}
Your help is appreciated
You need to serialize the collection you have in your webmethod to json and have the return method to return a string (= actually is the serialized output of your collection = json).
Look at Encosia. It propably has the most thorough articles about asp.net, javascript and jquery working together.
using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(CalendarEvents);
And I would rather used jquery to call webservices. And watch out for the d. prefix in asmx. webservices when consuming them with jquery.
Do you have a ScriptManager on that page's Master?
There are basically two keys to enabling the nice Namespace.Service.Method
syntax: MicrosoftAjax.js and a service-specific JavaScript proxy.
You get MicrosoftAjax.js automatically when you have a ScriptManager anywhere on the page (or it's Master(s)).
To get the JavaScript proxy, you'll need either a ServiceReference on the ScriptManager or a JavaScript include that points to where that's generated from. E.g.:
<asp:ScriptManagerProxy>
<Services>
<asp:ServiceReference Path="/CalendarHandler.asmx" />
</Services>
</asp:ScriptManagerProxy>
<script>
// Speak.CalendarHandler.GetEvents() will be available here.
</script>
All that actually does is inject the script that's available at /CalendarHandler.asmx/js
(or /jsdebug when running in debug). That's part of what the ScriptService
attribute on the service enables.
So, as long as MicrosoftAjax.js is already being injected from somewhere on the page, you could also simply add a script reference to that JavaScript proxy yourself:
<script src="/CalendarHandler.asmx/js"></script>
<script>
// Speak.CalendarHandler.GetEvents() will be available here.
</script>
Update:
If the service proxy generation code isn't respecting the Namespace
parameter in your [WebService]
attribute, can you namespace the service code with the namespace keyword instead?
namespace Speak
{
[ScriptService]
public class CalendarHandler : WebService
{
[WebMethod]
public IDictionary<DateTime, string> GetEvents()
{
// Etc.
}
}
}
I can confirm that this approach does affect the generated service proxy.
精彩评论