ajax request with .ashx
<p>I am trying to send the ajax request to handler.ashx on click of link.
I have checked all the ready states and status of the ajax request.
On click event handler.ashx gets called but response from the handler is not received.</p>

<p>when i remove condition "if (xhr.readystate==4) " and keep only "if (xhr.status == 200)" then it is working...</p>

<p>please have a look at following code and tell me what is going wrong???</p>

<p>thanks!!! :)</p>

<p>JS : </p>

<pre><code>&lt;script type="text/javascript"&gt;
    function setActive(element) 
    {

        element.style.color = '#ff0000';

        var links = document.getElementsByName("link");
        for (i = 0; i &lt; links.length; i++)
            if (links[i] != element) 
            {
                links[i].style.color = 'blue';
            }

            sendReq();


    }

    function sendReq()
    {


        var xhr;
        var gridData;
        var url;

        url = "Handler.ashx";
        xhr = GetXMLHttpRequestObject();

        xhr.onreadystatechange = function ()
         {

             if (xhr.readystate == 1 || xhr.readystate == 2 || xhr.readystate == 3) 
             {
                 document.getElementById("resText").innerHTML = "Loading....";
             }
            else if (xhr.readystate == 4 &amp;&amp; xhr.status==200) 
            {
                document.getElementById("resText").innerHTML = xhr.responseText;
             }

            else if (xhr.status == 404) 
              {
                  document.getElementById("resText").innerHTML = "Page Not Found";

              }
              else 
              {
                    document.getElementById("resText").innerHTML = "Error....";
              }

        } 

        xhr.open("POST", url, true);
        xhr.send(null);
    }


    function GetXMLHttpRequestObject() {

        var XHR; 
        var ua = navigator.userAgent.toLowerCase(); 
        try {

            if (!window.ActiveXObject) { 
                XHR = new XMLHttpRequest();
            }
            else {
                if (ua.indexOf('msie 5') == -1) { //IE 5.x
                    XHR = new ActiveXObject("Msxml2.XMLHTTP");
                }
                else { //IE 6.x and up  
                    XHR = new ActiveXObject("Microsoft.XMLHTTP");
                }
            } 

            if (XHR == null) {
                throw "Unable to instantiate the XMLHTTPRequest object.";
            }
        }
        catch (e) {
            alert("This browser does not appear to support AJAX functionality. error: " + e.name
          + " description: " + e.message);
        }
        return XHR;
    } 


    &lt;/script&gt;
</code></pre>

<p>.aspx file :</p>

<pre><code>&lt;body&gt;
&lt;form id="form1" runat="server"&gt;
&lt;div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a id="link1" name="link" href="javascript:void(0);" onclick="setActive(this);"&gt;Quick Links&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="link2" name="link" href="javascript:void(0);" onclick="setActive(this);"&gt;My Acount&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="link3" name="link" href="javascript:void(0);" onclick="setActive(this);"&gt;Job Post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="link4" name="link" href="javascript:void(0);" onclick="setActive(this);"&gt;Shortlisted Candidates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="link5" name="link" href="javascript:void(0);" onclick="setActive(this);"&gt;Selected Candidates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a id="link6" name="link" href="javascript:void(0);" onclick="setActive(this);"&gt;Logout&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;/div&gt;
&lt;div&gt;
    &lt;asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"&gt;
    &lt;div id="resText"&gt;&lt;/div&gt;
    &lt;/asp:ContentPlaceHolder&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
</code></pre>

<p>handler file : 
     &lt;%@ WebHandler Language="C#" Class="Handler" %></p>

<pre><code>using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";
    context.Response.Write("Hello World");
}

public bool IsReusable {
    get {
        return false;
    }
}

}
</code></pre>
