jquery ajax调用带有重定向模式的Web方法?

时间:2022-12-01 10:18:54

I have a piece of jquery that makes an ajax call to a server side webmethod

我有一个jquery,它对服务器端webmethod进行ajax调用

$("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "TestPage.aspx/TestString",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                }
            });
        });

This works fine if I have routing turned off

如果我关闭了路由,这可以正常工作

settings.AutoRedirectMode = RedirectMode.Off;

I'd like to have it on, but when I do, the ajax call fails with “401 (Unauthorized).” Is there a way I can make my ajax calls while still having routing on?

我想打开它,但是当我这样做时,ajax调用失败并显示“401(未经授权)。”有没有办法在仍然有路由的情况下进行ajax调用?

settings.AutoRedirectMode = RedirectMode.Permanent;

Edit: Some people have voted that this should be closed as a duplicate and that the answer is over here, but that answer doesn't help. The first solution it offers is to set RedirectMode to Off, which is exactly what I don't want to do, and the other bit about Friendly Urls doesn't work.

编辑:有些人投票认为这应该作为副本关闭,答案已经结束,但答案没有帮助。它提供的第一个解决方案是将RedirectMode设置为Off,这正是我不想做的,而另一个关于Friendly Urls的不起作用。

1 个解决方案

#1


4  

I can see your issue - disabling AutoRedirectMode means .aspx URLs are freely accessible (not good). But you want your WebMethod accessible using the ASPX url.

我可以看到你的问题 - 禁用AutoRedirectMode意味着.aspx URL可以*访问(不好)。但是您希望使用ASPX URL访问WebMethod。

A work around to this is to disable auto redirect mode as shown above

解决此问题的方法是禁用自动重定向模式,如上所示

settings.AutoRedirectMode = RedirectMode.Off;

And handle requests containing .aspx pages yourself in the page_load (not called when hitting your WebMethod).

并在page_load中处理包含.aspx页面的请求(在点击WebMethod时不会调用)。

protected void Page_Load(object sender, EventArgs e)
{
   if(Request.RawUrl.Contains(".aspx"))
   {
      Response.StatusCode = 404;
      Server.Transfer("page-not-found.aspx");
   }
}

If you want to redirect as per normal instead of sending a 404 - strip the .ASPX and send a 301 using :

如果你想按照正常情况重定向而不是发送404 - 剥离.ASPX并使用以下命令发送301:

Response.RedirectPermanent(Request.RawUrl.Replace(".aspx",""));

If you use a MasterPage or inherit from page you'll only need to write the code once.

如果您使用MasterPage或从页面继承,您只需要编写一次代码。

#1


4  

I can see your issue - disabling AutoRedirectMode means .aspx URLs are freely accessible (not good). But you want your WebMethod accessible using the ASPX url.

我可以看到你的问题 - 禁用AutoRedirectMode意味着.aspx URL可以*访问(不好)。但是您希望使用ASPX URL访问WebMethod。

A work around to this is to disable auto redirect mode as shown above

解决此问题的方法是禁用自动重定向模式,如上所示

settings.AutoRedirectMode = RedirectMode.Off;

And handle requests containing .aspx pages yourself in the page_load (not called when hitting your WebMethod).

并在page_load中处理包含.aspx页面的请求(在点击WebMethod时不会调用)。

protected void Page_Load(object sender, EventArgs e)
{
   if(Request.RawUrl.Contains(".aspx"))
   {
      Response.StatusCode = 404;
      Server.Transfer("page-not-found.aspx");
   }
}

If you want to redirect as per normal instead of sending a 404 - strip the .ASPX and send a 301 using :

如果你想按照正常情况重定向而不是发送404 - 剥离.ASPX并使用以下命令发送301:

Response.RedirectPermanent(Request.RawUrl.Replace(".aspx",""));

If you use a MasterPage or inherit from page you'll only need to write the code once.

如果您使用MasterPage或从页面继承,您只需要编写一次代码。