programing

AspNet을 사용하여 jquery.ajx에서 호출 웹 메서드를 사용하는 동안 인증에 실패했습니다.친절한 얼스와 아스프넷입니다.신원

instargram 2023. 10. 24. 20:07
반응형

AspNet을 사용하여 jquery.ajx에서 호출 웹 메서드를 사용하는 동안 인증에 실패했습니다.친절한 얼스와 아스프넷입니다.신원

jQuery에서 web method를 호출하면.Nuget 패키지가 설치된 Ajax Microsoft.AspNet.FriendlyUrls v 1.0.2 및 Microsoft.AspNet.ID v.1.0.0.을(를) 입력하면 data.d는 없지만 'Authentication failed' 속성 메시지가 표시됩니다.

내 Webmethod.aspx는 다음과 같습니다.

<!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>

WebMethod.aspx.cs 의 나의 웹 메소드는 다음과 같습니다.

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

Global.asax.cs 에서 라우팅이 활성화됨

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

App_Start에서 경로를 등록했습니다.

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}

App_Start 폴더의 RouteConfig...

다음 행을 주석으로 달거나 리디렉션 모드를 변경합니다.

//settings.AutoRedirectMode = RedirectMode.Permanent;

동일한 사용 사례는 아니지만 웹 서비스를 사용하고 있으며 "Authentication failed"(인증 실패) 메시지가 표시된 경우 언급할 가치가 있습니다.귀하의 웹 서비스가 "[시스템] 속성을 가지고 있는지 확인하시기 바랍니다.웹.스크립트.서비스.ScriptService]".이렇게 하면 스크립트에서 웹 서비스를 호출할 수 있습니다.

예:

[System.Web.Script.Services.ScriptService]
public class WS_Default : System.Web.Services.WebService
{
    ...
}

언급URL : https://stackoverflow.com/questions/20032240/authentication-failed-during-call-webmethod-from-jquery-ajx-with-aspnet-friendly

반응형