Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ajax - Authentication failed during call webmethod from jquery.ajx with AspNet.FriendlyUrls and AspNet.Identity

If I call webmethod from jQuery.Ajax with installed Nuget packages Microsoft.AspNet.FriendlyUrls v 1.0.2 and Microsoft.AspNet.Identity v.1.0.0., then I get the data object, but without data.d but with property Message 'Authentication failed'.

My Webmethod.aspx is:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     <title>WebMethod</title>
    <script src="Scripts/jquery-2.0.3.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <h3>Test Webmethod</h3>
    <div id="greeitng"></div>
    <div id="innerError" style="border:1px dotted red;display:none;" title="errorMessage"></div>
    <script type="text/javascript">
        function asyncServerCall(username) {
            jQuery.ajax({
                url: 'WebMethod.aspx/HelloWorld',
                type: "POST",
                data: "{'username':'" + username + "'}",
                //async: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data.d == undefined)
                        document.getElementById("greeitng").innerHTML = data.Message;
                    else
                        document.getElementById("greeitng").innerHTML = data.d;
                },
                error: function (err) {
                    if (err.responseText) {
                        $('#innerError').html(err.responseText).show();
                    }
                    else {
                        alert(err);
                    }
                }
            });
        }
        $(document).ready(function () {
            $('#innerError').hide();
            asyncServerCall("Superuser");
        });
    </script>
    </form>
</body>
</html>

My Webmethod in WebMethod.aspx.cs is:

[System.Web.Services.WebMethod]
public static string HelloWorld(string username)
{
    return string.Format("Hello, {0}", username);
}

In Global.asax.cs is Routing activated

void Application_Start(object sender, EventArgs e)
{
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

In App_Start have regitred the routes

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your App_Start folder, within your RouteConfig....

Comment out the following line or change its RedirectMode:

//settings.AutoRedirectMode = RedirectMode.Permanent;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...