ajax webservice asmx 401未经授权的错误

时间:2016-10-19 21:46:56

标签: jquery asp.net ajax web-services asmx

我有一个内部网站需要登录,人们可以在会议期间记录笔记和其他信息。我试图通过AJAX调用将数据发布到同一服务器上同一文件夹中的Web服务。我收到401未经授权的错误。

使用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文件:

<%@ 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);
    }

}

我正在使用表单身份验证(对AD进行身份验证):

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

我以前从未在安全环境中进行过ajax调用,让我知道你需要的其他信息。

我已经研究过类似问题的其他解决方案但无法让它们发挥作用。

1 个答案:

答案 0 :(得分:0)

我明白了。

    public static string AutoSave(int ID, string Note)

应该是:

    public string AutoSave(int ID, string Note)