首页 > 解决方案 > 为什么对 webmethod 的 ajax 调用从后面的代码返回 401?

问题描述

阿贾克斯

function verify() {
$.ajax({
    type: "POST",
    url: "Register.aspx/verifyImage",
    data: '{text: "' + $("#<%=TextBox4.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        alert(response.d)
    },
    failure: function(response) {
        alert(response.d);
    }
});
}

C# 网络方法

[WebMethod]
    public void verifyImage(string text)
    {
        string imageString = Session["imageString"].ToString();
        if (imageString.Equals(text))
        {
            Response.Write("<script>alert('Memeber Page');</script>");
        }
        else
        {
            Response.Write("<script>alert('Incorrect');</script>");
        }
    }

页面控件

<div style="height: 50px">
    <asp:Image ID="Image1" runat="server" />
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <input id="Button1" type="button" value="Submit" onclick="verify()"/>
</div>

调试输出

"name":"POST /Register.aspx/verifyImage","duration":"00:00:00.0161020","success":true,"responseCode":"401","url":"http://localhost:54506/Register.aspx/verifyImage","properties":{"DeveloperMode":"true","_MS.ProcessedByMetricExtractors":"(Name:'Requests', Ver:'1.0')

我是 ajax 新手,正在构建一个图像验证器页面。我想使用 ajax 将图像文本与 asp 文本框文本进行比较,以将文本框文本发送到后面代码中的 web 方法。在输出中,ajax 调用写入success: true,但返回401(未经授权)代码。我究竟做错了什么?在这一点上,我只是想让电话开始工作,稍后将填写 webmethod。

标签: c#asp.netajaxwebforms

解决方案


推荐阅读