as practicing Ajax, I wrote a Ajax call that sends to a local asp.net Handler.ashx a Json with a username and password, and returns true if they are equal to "jhon" "123456", else false. I debugged the code, and I saw that the Handler.ashx receives the call, does the validation, and write to response, but the Ajax call not success.

this the Ajax call:

  $.ajax(
        {
            url: '/Handler.ashx',
            dataType: 'json',
            data: {
                name: document.getElementById("username").value,
                password: document.getElementById("password").value
            },
            success: function (json) {
                alert(json.isvalid);
            }
        });
        alert("failed");

and this is the server side:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        Console.WriteLine("check");
        var name = context.Request["name"];
        var password = context.Request["password"];

        string response = IsValid(name, password) ? "true" : "false";
        context.Response.ContentType = "appliaction/text";
        context.Response.Write("{isvalid:'" + response + "'}");
    }

    private bool IsValid(string name, string password)
    {
        Console.WriteLine("isvalid");
        return (name == "jhon" && password == "123456");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

thanks!

Related posts

Recent Viewed