参数从代码隐藏到jquery [母版页]

发布于 2024-08-22 05:38:29 字数 1068 浏览 5 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

拥抱影子 2024-08-29 05:38:29

webmethod 属性的作用是说这个方法应该响应某个 url(有点像 asp.net mvc 中的路由)。由于我不使用网络表单,所以我真的不知道它在决定方法应该响应哪个 url 时使用什么逻辑。但我的猜测是,url 应该类似于“Master.cs/UserStatus”(不确定 .cs 扩展名)。这当然是一个相对 url,因此您可以尝试这样的操作: <%=ResolveUrl("~/Master.cs/UserStatus")%> (如果母版页位于您的根文件夹)。那么你的例子应该是这样的:

$.ajax({
        type: "POST",
        url: '<%=ResolveUrl("~/Master.cs/UserStatus")%>',
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: AjaxSucceeded,
        error: AjaxFailed
    });

更新

.cs 扩展名可能是错误的。但我认为无论如何您都不应该将其放在母版页中。如果您想使用 ajax,您可能应该将其放在 Web 服务或 .ashx 处理程序或其他内容中。但是根据您最后的评论,您似乎不需要使用ajax(如果您不需要,则不应该)。您在评论中编写的代码中的问题可能是 id 错误(请记住您需要 JavaScript 中的客户端 id)。

但我可能会这样做:

<script type="text/javascript">
    var statuses = [];
    <%foreach(var status in UserStatus()) {%>
        statuses.push(<%=status%>);
    <%}%>
</script>

这将在浏览器中呈现此 javascript:

<script type="text/javascript">
    var statuses = [];

        statuses.push("#ll1");
        statuses.push("#ll2");

</script>

然后您将在 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:

$.ajax({
        type: "POST",
        url: '<%=ResolveUrl("~/Master.cs/UserStatus")%>',
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: AjaxSucceeded,
        error: AjaxFailed
    });

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:

<script type="text/javascript">
    var statuses = [];
    <%foreach(var status in UserStatus()) {%>
        statuses.push(<%=status%>);
    <%}%>
</script>

This will render this javascript in the browser:

<script type="text/javascript">
    var statuses = [];

        statuses.push("#ll1");
        statuses.push("#ll2");

</script>

Then you will have your statuses in the statuses array.

洒一地阳光 2024-08-29 05:38:29

正如 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.

落墨 2024-08-29 05:38:29

如果您还没有这样做,您将需要将 [ScriptService] 属性添加到您的 webmethod 中,如下所示

表示Web服务可以是
从脚本调用

请参阅 ScriptServiceAttribute

If you've not done so you will need to add the [ScriptService] attribute to your webmethod as this

Indicates that a Web service can be
invoked from script

See ScriptServiceAttribute

不…忘初心 2024-08-29 05:38:29

我认为问题是您尝试发布到 .cs 文件。由于安全原因,ISS 不提供扩展名 .cs。因此,即使您的方法存在于代码隐藏文件中,您也必须发布到 .aspx 文件。 ASP.NET 将为您完成剩下的工作。
所以尝试:

$.ajax({
         type: "POST",
        url: "/Master.Master.aspx/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");
}

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:

$.ajax({
         type: "POST",
        url: "/Master.Master.aspx/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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文