参数从代码隐藏到jquery [母版页]
我正在使用 JQuery 在母版页中创建菜单。我使用 $.ajax({}); 将链接的 id 传递给 jquery
问题:
Getting failed: Showing error message in AjaxFailed(result) function.
代码:html[JQuery]
$.ajax({
type: "POST",
url: "Master.Master.cs/UserStatus",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
function AjaxSucceeded(result) {
if (result.d.length != 0) {
for (var i = 0; i < result.d.length; i++) {
$(result.d[i]).hide();
}
}
}
function AjaxFailed(result) {
alert("Error");
}
c# 代码:Codebehind
private static List<string> xx;
[WebMethod]
public static List<string> UserStatus()
{
return xx;
}
protected void Page_Load(object sender, EventArgs e)
{
xx = new List<string> {"#ll1", "#ll2" };
}
I am creating Menu in the master page using JQuery. i am passing the id of the link to jquery using $.ajax({});
Problem:
Getting failed: Showing error message in AjaxFailed(result) function.
Code:html[JQuery]
$.ajax({
type: "POST",
url: "Master.Master.cs/UserStatus",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
function AjaxSucceeded(result) {
if (result.d.length != 0) {
for (var i = 0; i < result.d.length; i++) {
$(result.d[i]).hide();
}
}
}
function AjaxFailed(result) {
alert("Error");
}
c# Code:Codebehind
private static List<string> xx;
[WebMethod]
public static List<string> UserStatus()
{
return xx;
}
protected void Page_Load(object sender, EventArgs e)
{
xx = new List<string> {"#ll1", "#ll2" };
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
webmethod 属性的作用是说这个方法应该响应某个 url(有点像 asp.net mvc 中的路由)。由于我不使用网络表单,所以我真的不知道它在决定方法应该响应哪个 url 时使用什么逻辑。但我的猜测是,url 应该类似于“Master.cs/UserStatus”(不确定 .cs 扩展名)。这当然是一个相对 url,因此您可以尝试这样的操作:
<%=ResolveUrl("~/Master.cs/UserStatus")%>
(如果母版页位于您的根文件夹)。那么你的例子应该是这样的:更新
.cs 扩展名可能是错误的。但我认为无论如何您都不应该将其放在母版页中。如果您想使用 ajax,您可能应该将其放在 Web 服务或 .ashx 处理程序或其他内容中。但是根据您最后的评论,您似乎不需要使用ajax(如果您不需要,则不应该)。您在评论中编写的代码中的问题可能是 id 错误(请记住您需要 JavaScript 中的客户端 id)。
但我可能会这样做:
这将在浏览器中呈现此 javascript:
然后您将在
statuses
数组中看到您的状态。What the webmethod attribute does is to say that this method should respond to a certain url (a little bit like routing in asp.net mvc). As I don't use webforms I don't really know what logic it uses when it decides what url the method should respond to. But my guess is that the url should be something like "Master.cs/UserStatus" (not sure about the .cs extension). And that is of course a relative url, so you can try something like this:
<%=ResolveUrl("~/Master.cs/UserStatus")%>
(if the masterpage is in your root folder). Then your example should be something like this:Update
The .cs extension is probably wrong. But I don't think you should have that in a master page anyway. You should probably have it in a web service or in a .ashx handler or something if you want to use ajax. But with you last comment it seems that you don't need to use ajax (and if you don't need that, you shouldn't). The problem in the code you wrote in the comment is probably that the id is wrong (remember that you need the client id in javascript).
But I would probably do it something like this:
This will render this javascript in the browser:
Then you will have your statuses in the
statuses
array.正如 Andre 和 Mattias 提到的,不提供 .cs 扩展名,因此您必须使用 .aspx 扩展名才能访问 WebMethod。
我在您的示例中看到的问题是,您将该方法放置在 MasterPage(具有 .master 扩展名)中,该方法也未提供服务,因此您无法从中调用 Web 方法。
您可以使用的解决方法是在从 Page 继承的类中定义它,并让所有页面都从该类继承。由于它是公共方法,因此它将在您的所有页面上公开,因此可用。基本上,是项目页面的基页。在这种情况下,您只需使用当前页面的地址即可进行呼叫。仅当您将在每个页面上使用它(例如菜单)时,这才有用。
您可以使用的第二个解决方法是在项目中放置的 .asmx Webservice 中定义 WebMethod。它的工作方式就像在页面上调用 WebMthod 一样,只是您必须使用 .asmx Webservice 的地址而不是页面的地址来进行调用。
Like Andre and Mattias mentioned, the .cs extension is not served, so you would have to use a .aspx extension to get to the WebMethod.
The problem I see in your example is that you are placing the method in the MasterPage (which would have a .master extension) which is also not served, so you can't call the web method from it.
A workaround you could use is to define it in a class that inherits from Page, and have all of your pages inherit from that class. Since its a public method, it will be public on all of your pages and therefore available. Basicly, a base page for your project's pages. In that case you would only need to use your current page's address to make the call. This is only usefull if it's something you will use on every page, like a menu.
A second workaround you can use is to define the WebMethod in a .asmx Webservice placed in the project. It would work like calling the WebMthod on a page, only you would have to use the .asmx Webservice's address instead of the page's to make the call.
如果您还没有这样做,您将需要将 [ScriptService] 属性添加到您的 webmethod 中,如下所示
请参阅 ScriptServiceAttribute
If you've not done so you will need to add the [ScriptService] attribute to your webmethod as this
See ScriptServiceAttribute
我认为问题是您尝试发布到 .cs 文件。由于安全原因,ISS 不提供扩展名 .cs。因此,即使您的方法存在于代码隐藏文件中,您也必须发布到 .aspx 文件。 ASP.NET 将为您完成剩下的工作。
所以尝试:
I think that the problem is that you try to post to the .cs file. The extension .cs is not served by ISS due to security reasons. So even though your method lifes in the code behind file, you have to post to the .aspx file. ASP.NET will do the rest for your.
So try: