如何在 Sharepoint 2010 中使用 HTTP 处理程序发出 jQuery AJAX 请求?
我正在开发一个 SP 2010 Visual Web Part,需要通过 AJAX 加载一些数据。
我发现这篇非常有用的文章 但我对此有一些疑问。
我有 Visual Web Part 项目,里面有 Visual Web Part 和一些其他文件。该文章说我需要创建一个 .ashx 文件来充当 HTTP 处理程序。 这就是我所做的,我在项目的根目录中创建了一个“MyHandler.ashx”并放置了 WebHandler
指令。但我不确定 Assembly
指令。
然后它说我们需要创建处理程序的实现并将其指向它。我相信我那部分没问题。
然后我需要通过 jQuery 进行实际的客户端调用。这是我不知道该怎么做的部分,因为我不知道部署 .ashx 的 url(如果它正在部署的话......)。
我正在尝试:
$.get('/_layouts/SomeNamespace/MyHandler.ashx', ...)
但它不起作用...再次,不知道处理程序的网址...
我在 Sharepoint 开发方面还很新,所以请不要严厉 =)
谢谢!
这是我的处理程序文件的简化版本。
MyHandler.ashx
<%@ WebHandler Language="C#" Class="SomeNamespace.MyHandler" CodeBehind="MyHandler.cs" %>
MyHandler.cs
namespace SomeNamespace
{
public class MyHandler : IHttpHandler
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context)
{
context.Response.Write("From the handler at " + DateTime.Now);
}
// bunch of methods
}
}
I'm developing a SP 2010 Visual Web Part that needs to load some data via AJAX.
I found this very helpful article but I'm having some doubts about it.
I have the Visual Web Part project, inside I have the Visual Web Part and some other files. The article says I need to create an .ashx file that will acts as an HTTP handler.
So that's what I did, I created a "MyHandler.ashx" in the root of the project and put the WebHandler
directive. But I'm not sure about the Assembly
directive.
Then it says we need to create an implementation to the handler and point it to it. I believe I have that part ok.
Then I need to make the actual client call via jQuery. Here's the part I don't know how to do it because I don't know the url where the .ashx is being deployed (if it's even being deployed...).
I'm trying:
$.get('/_layouts/SomeNamespace/MyHandler.ashx', ...)
But it's not working... again, don't know the handler's url...
I'm pretty new at Sharepoint development, so please don't be harsh =)
Thanks!
Here are simplified versions of my handler's files.
MyHandler.ashx
<%@ WebHandler Language="C#" Class="SomeNamespace.MyHandler" CodeBehind="MyHandler.cs" %>
MyHandler.cs
namespace SomeNamespace
{
public class MyHandler : IHttpHandler
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context)
{
context.Response.Write("From the handler at " + DateTime.Now);
}
// bunch of methods
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将 MyHandler.ashx 部署到 _layouts/SomeNamespace/ 文件夹。使用添加 -> Visual Studio 解决方案资源管理器中的SharePoint“_Layouts”映射文件夹。
You have to deploy MyHandler.ashx to _layouts/SomeNamespace/ folder. Use Add -> SharePoint "_Layouts" Mapped Folder in Visual Studio Solution Explorer.