2010-07-05 117 views
3

生病用asp.net许可疯狂... 这个时候,我只是不能AJAX-CALL任何种类的WebMethod,或者我只是得到:ASP.NET WEBMETHOD总是返回401

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

代码:

<WebMethod(True)> _ 
    Public Function Login(ByVal usuario As String, ByVal senha As String) As Boolean 
     [lots of validations] 
     If (con.Connection.State = ConnectionState.Open) Then 
      Return True 
     Else 
      Return False 
     End If 
    End Function 

JQUERY CALL:

$("#btnEnviar").click(function() { 
      $('#login').hide(); 
      $('#ajaxLoader').fadeIn(); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "Login.aspx/Login", 
       data: "{'usuario':'" + $('#txtUsuario').val() + "','senha':'" + $('#txtSenha').val() + "'}", 
       dataType: "json", 
       dataFilter: function(data) { 
        var msg = eval('(' + data + ')'); 
        if (msg.hasOwnProperty('d')) 
         return msg.d; 
        else 
         return msg; 
       }, 
       success: function(msg) { 
        if (msg.UsuarioValido == '1') { 
         top.location = "Home.aspx" 
        } 
        else { 
         $('#ajaxLoader').hide(); 
         $('#login').fadeIn(); 
        } 
       } 
     }); 

上有一些错误我知道成功的东西。这不是现在的问题。 Firebug控制台总是返回401未授权当我尝试ajax调用。

有人吗?

+1

你是如何做的电话吗? – 2010-07-05 23:26:56

+0

编辑了主帖。 – ale 2010-07-05 23:31:23

+0

你在web配置中设置了什么认证? – redsquare 2010-07-05 23:32:35

回答

10
  1. WEBMETHOD应该共享(VB)/静态(C#),如果你得到错误500,纪念你的方法共享/静态和你做。

  2. 有关错误401:如果您使用窗体身份验证记得要允许您的登录页面匿名访问,通过在你的web.config这样做:

    <location path="Login.aspx"> 
        <system.web> 
        <authorization> 
         <allow users="*"/> 
        </authorization> 
        </system.web> 
    </location>