简体   繁体   English

ajax webservice asmx 401未经授权的错误

[英]ajax webservice asmx 401 unauthorized error

I have an internal website that requires a login where people can record notes and other information during meetings. 我有一个内部网站,要求登录后,人们才能在会议期间记录笔记和其他信息。 I am trying to post the data with an AJAX call to a webservice on the same server, in the same folder. 我正在尝试使用AJAX调用将数据发布到同一服务器上同一文件夹中的Web服务。 I am getting an 401 unauthorized error. 我收到401未经授权的错误。

Javascript: Javascript:

function saveNotes()
{
    var json = "{ ID: 5, Note: 'Test Note'}";
    $.ajax({
        type: "POST",
        url: "AutoSaveService.asmx/AutoSave",
        data: json,
        xhrFields: { withCredentials: true },
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (r) {
            var data = JSON.parse(r.d);
            console.log(data.M + ":" + data.N)
        },
        error: function (r) { console.log(r.responseText); },
        failure: function (r) { console.log(r.responseText); }
    });
}

WebService asmx file: WebService asmx文件:

<%@ WebService Language="C#" Class="AutoSaveService" %>

[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoSaveService : System.Web.Services.WebService
{
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public static string AutoSave(int ID, string Note)
    {
        var data = new { M = ID, N = Note };
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

        return js.Serialize(data);
    }

}

I am using forms authentication (authenticating vs AD): 我正在使用表单身份验证(身份验证与广告):

<authentication mode="Forms">
  <forms loginUrl="login.aspx" name="adAuthCookie" timeout="120" path="/"/>
</authentication>

I have never done ajax calls within a secure environment before, let me know what other information you need. 我以前从未在安全的环境中进行过ajax调用,请让我知道您还需要什么其他信息。

I've looked at other solutions to similar problems but can't get them to work. 我已经研究了类似问题的其他解决方案,但无法使其正常工作。

I figured it out. 我想到了。

    public static string AutoSave(int ID, string Note)

should be: 应该:

    public string AutoSave(int ID, string Note)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM