Asmx Web服务引用问题
我需要在客户端页面中使用 JavaScript 调用我的 Web 服务方法。我认为我没有正确引用这一点,希望您能帮助解决这一问题。
错误消息显示“CalendarHandler 未定义”。
<%@ 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 () {
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;
}
}
感谢您的帮助
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 () {
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 webmethod 中的集合序列化为 json,并使用 return 方法返回字符串(= 实际上是集合的序列化输出 = json)。
查看 Encosia。它可能包含有关 asp.net、javascript 和 jquery 协同工作的最详尽的文章。
而且我宁愿使用jquery来调用webservices。并留意 d。 asmx 中的前缀。使用 jquery 使用 Web 服务时。
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.
And I would rather used jquery to call webservices. And watch out for the d. prefix in asmx. webservices when consuming them with jquery.
您在该页面的 Master 上有 ScriptManager 吗?
启用良好的
Namespace.Service.Method
语法基本上有两个关键:MicrosoftAjax.js 和特定于服务的 JavaScript 代理。当页面上的任何位置有 ScriptManager(或其 Master)时,您会自动获得 MicrosoftAjax.js。
要获取 JavaScript 代理,您需要 ScriptManager 上的 ServiceReference 或指向生成位置的 JavaScript 包含文件。例如:
实际上所做的就是注入
/CalendarHandler.asmx/js
中可用的脚本(或在调试中运行时为 /jsdebug)。这是服务上的ScriptService
属性所实现的功能的一部分。因此,只要 MicrosoftAjax.js 已经从页面上的某个位置注入,您也可以简单地自己添加对该 JavaScript 代理的脚本引用:
更新:
如果服务代理生成代码不尊重
Namespace<您的
[WebService]
属性中的 /code> 参数,您可以使用 namespace 关键字来命名服务代码吗?我可以确认这种方法确实会影响生成的服务代理。
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.:
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 theScriptService
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:
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?I can confirm that this approach does affect the generated service proxy.