从 jQuery 到 Sharepoint 的页面方法调用

发布于 2024-11-05 13:46:01 字数 783 浏览 11 评论 0原文

我在 MS SharePoint 2010 的 _LAYOUTS 文件夹中部署了一个应用程序页面 (aspx)。

我想使用以下命令调用该页面内标有 [WebMethod] 属性的方法jQuery。我在 document.ready() 上使用以下代码:

$("#btnOk").click(function () {
    var theUrl = '/_layouts/MyProject/MyPage.aspx/MyMethod';
    $.ajax({
        type: "get",
        dataType: "json",
        url: theUrl,
        data: {},
        success: function (response) {
            [...]
        },
        error: function (xhr, textStatus, errorThrown) {
            [...]
        }
    });
});

不幸的是,该代码不起作用。问题出在 URL 上:事实上,如果我使用像这样的绝对 URL,它就可以工作

var theUrl = 'http://server/sites/xxx/_layouts/MyProject/MyPage.aspx/MyMethod';

How can I Transform my path in a Absolute URL?

I have an application page (aspx) deployed in the _LAYOUTS folder of MS SharePoint 2010.

I would like to call a method inside that page marked with the [WebMethod] attribute using jQuery. I am using the following code on document.ready():

$("#btnOk").click(function () {
    var theUrl = '/_layouts/MyProject/MyPage.aspx/MyMethod';
    $.ajax({
        type: "get",
        dataType: "json",
        url: theUrl,
        data: {},
        success: function (response) {
            [...]
        },
        error: function (xhr, textStatus, errorThrown) {
            [...]
        }
    });
});

This code unfortunately does not work. The problem is with the URL: in fact it works if I use an absolute URL like this

var theUrl = 'http://server/sites/xxx/_layouts/MyProject/MyPage.aspx/MyMethod';

How can I transform my path in an absolute one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撞了怀 2024-11-12 13:46:01
/_layouts/MyProject/MyPage.aspx/MyMethod

在你的例子中相当于:

http://server/_layouts/MyProject/MyPage.aspx/MyMethod

那是你的问题。以 / 开头表示从根开始。你应该调整这个。如果它必须以某种方式动态,因为它可以在多个地方使用,您可能需要使用代码隐藏来注入路径或其他东西。如果始终从静态位置运行,只需修改 url 即可。

例如,如果您运行它的页面是:

http://server/sites/xxx/Somepage.aspx

那么只需将其更改为

_layouts/MyProject/MyPage.aspx/MyMethod

不带斜线。

例如,如果您位于子文件夹中:

http://server/sites/xxx/Pages/Somepage.aspx

那么您可以执行以下操作:

../_layouts/MyProject/MyPage.aspx/MyMethod

.. 将带您进入一个文件夹。

/_layouts/MyProject/MyPage.aspx/MyMethod

in your example is equivalent to:

http://server/_layouts/MyProject/MyPage.aspx/MyMethod

that is your problem. starting with / means start at the root. You should adjust this. If it has to be somehow dynamic because it can be used multiple places, you may need to use the codebehind to inject the path or something. If this is always run from a static place, just modify the url.

If the page you're running it from is, for instance:

http://server/sites/xxx/Somepage.aspx

Then just change it to

_layouts/MyProject/MyPage.aspx/MyMethod

with no slash.

If you are in a subfolder, for instance:

http://server/sites/xxx/Pages/Somepage.aspx

then you can do this:

../_layouts/MyProject/MyPage.aspx/MyMethod

the .. will take you up one folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文