简体   繁体   English

401:unauthorized 当我尝试在 .net 中通过 AJAX 调用方法时

[英]401:unauthorized when I try to call a method via AJAX in .net

I have a .net project in the framework 4.5.1我在框架 4.5.1 中有一个 .net 项目

When I use ajax like below, it works and the debugger hits the page_load event:当我像下面这样使用ajax ,它可以工作并且调试器会触发page_load事件:

$.ajax({
        "async": true,
        "crossDomain": true,
        type: "POST",
        url: "Advanced.aspx",
        contentType: "application/json; charset=utf-8",
        success: function (ms) {
            alert('success');
        }
    })

In my advanced.aspx.cs , I have the method below:在我的advanced.aspx.cs 中,我有以下方法:

[System.Web.Services.WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public static string SaveChange(string myval)
        {
            //dummy code for test purposes.
            string u = myval + "X";
            return u;
        }

I want to hit this method via ajax .我想通过ajax这个方法。 To do this, I am updating my ajax call like below:为此,我正在更新我的ajax调用,如下所示:

$.ajax({
            "async": true,
            "crossDomain": true,
            type: "POST",
            url: "Advanced.aspx/SaveChange",
            data: {username:"test"},
            contentType: "application/json; charset=utf-8",
            success: function (ms) {
                alert('success');
            }
        })

However, it gives me 401:unauthorized error when i do it in that way, shown below:但是,当我这样做时,它给了我401:unauthorized error ,如下所示:

在此处输入图片说明

How can I fix this?我怎样才能解决这个问题? Tried lots of things for the last few hours, no luck.. Any help would be appreciated.在过去的几个小时里尝试了很多东西,没有运气.. 任何帮助将不胜感激。

EDIT: I've used async and crossDomain hoping to fix this issue, didn't help.编辑:我使用asynccrossDomain希望解决这个问题,没有帮助。 Normally they don't exist in my ajax call, nothing changes.通常它们在我的 ajax 调用中不存在,没有任何变化。

Try updating your AJAX JS to:尝试将您的 AJAX JS 更新为:

$.ajax({
    type: "POST",
    url: "Advanced.aspx/SaveChange",
    data: {myval:"test"},
    success: function (ms) {
        alert('success');
    }
})

Found the answer here: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"在这里找到答案: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"

I have used settings.AutoRedirectMode = RedirectMode.Off;我用过settings.AutoRedirectMode = RedirectMode.Off; instead of RedirectMode.Permanent in ~/App_Start/RouteConfig.cs , which fixed the issue.而不是RedirectMode.Permanent~/App_Start/RouteConfig.cs ,其中固定的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在启用了窗口身份验证的情况下对asp.net Web API端点进行Ajax调用时出现401未经授权的错误 - 401 Unauthorized error when making ajax call to asp.net web api endpoint with window authentication enabled 当我尝试获取数据时,Kinvey显示“ 401未经授权” - Kinvey shows 401 Unauthorized when I try to fetch data Ajax呼叫不再起作用(未授权401) - Ajax call no longer working (401 unauthorized) 当我尝试使用带有沙箱的本地主机中的 php file_get_contents() 获取 OAuth2 令牌时,Paypal Rest Api 返回 401 Unauthorized ,但在 ajax 中有效 - Paypal Rest Api returns 401 Unauthorized when i try to get OAuth2 token using php file_get_contents() in localhost with sandbox , but works in ajax jquery $ .ajax调用在Chrome或Firefox中导致401未经授权的响应,但在IE中有效 - jquery $.ajax call results in 401 unauthorized response when in Chrome or Firefox, but works in IE AJAX Jquery 调用返回 401(未经授权)- API 调用 - AJAX Jquery Call returning 401 (unauthorized) - API Call 尝试通过控制台登录时出现401未经授权的错误 - 401 unauthorized error when trying to login via console jQuery Ajax未经授权的401错误 - jQuery Ajax Unauthorized 401 Error $ .ajax请求始终为401(未经授权) - $.ajax request always 401 (UNAUTHORIZED) 我正面临一个错误 net::ERR_ABORTED 401(未经授权) - I am facing an error net::ERR_ABORTED 401 (Unauthorized)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM