0

ajax

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

[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>");
        }
    }

Page controls

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

Debug Output

"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')

I'm new to ajax and am building an image verifier page. I want to compare the image text to asp textbox text using ajax to send the textbox text to a web method in the code behind. In the output, the ajax call writes success: true, but returns a 401 (unauthorized) code. What am I doing wrong? At this point I'm just trying to get the call to work and will fill in the webmethod more later.

Ian
  • 105
  • 9

0 Answers0