从 jQuery 到 Sharepoint 的页面方法调用
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的例子中相当于:
那是你的问题。以 / 开头表示从根开始。你应该调整这个。如果它必须以某种方式动态,因为它可以在多个地方使用,您可能需要使用代码隐藏来注入路径或其他东西。如果始终从静态位置运行,只需修改 url 即可。
例如,如果您运行它的页面是:
那么只需将其更改为
不带斜线。
例如,如果您位于子文件夹中:
那么您可以执行以下操作:
.. 将带您进入一个文件夹。
in your example is equivalent to:
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:
Then just change it to
with no slash.
If you are in a subfolder, for instance:
then you can do this:
the .. will take you up one folder.