ASP.NET调用的WebMethod jQuery的AJAX" 401" [英] ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"

查看:437
本文介绍了ASP.NET调用的WebMethod jQuery的AJAX" 401"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直坚持这个好几个小时

Been stuck with this for hours

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

我试图调用这个WebMethod的在我的ASP.Net的Webform

I'm trying to call this WebMethod in my ASP.Net Webform

[WebMethod]
public static string GetClients(string searchTerm, int pageIndex)
{
    string query = "[GetClients_Pager]";
    SqlCommand cmd = new SqlCommand(query);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
    cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
    cmd.Parameters.AddWithValue("@PageSize", PageSize);
    cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
    return GetData(cmd, pageIndex).GetXml();
}

从这个jquery.ajax

From this jquery.ajax

function GetClients(pageIndex) {
    $.ajax({
        type: "POST",
        url: "ConsultaPedidos.aspx/GetClients",
        data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
    });
}

不过,我总是得到这样的错误:

But I always get this error:

POST 的http://本地主机:64365 / ConsultaPedidos.aspx / GetClients 401   (未经授权)

POST http://localhost:64365/ConsultaPedidos.aspx/GetClients 401 (Unauthorized)

奇怪的是,这个曾经工作,直到我开始验证用户

Weird thing is that this used to work until I start authenticating users

<system.web>
...
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="/Dashboard" />
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
...
</system.web>

任何想法?

推荐答案

这是推动我疯了。

〜/ App_Start / RouteConfig.cs 更改:

settings.AutoRedirectMode = RedirectMode.Permanent;

要:

settings.AutoRedirectMode = RedirectMode.Off;

(或者只是注释行)

(Or just comment the line)

此外,如果启用了友好的URL,你需要改变

Also if friendly URLs are enabled you need to change

url: "ConsultaPedidos.aspx/GetClients",

要:

url: '<%= ResolveUrl("ConsultaPedidos.aspx/GetClients") %>',

希望这有助于别人

Hope this help somebody else

这篇关于ASP.NET调用的WebMethod jQuery的AJAX&QUOT; 401&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆