使用.ashx的ajax请求

时间:2012-04-16 07:48:23

标签: asp.net

我正在尝试通过点击链接将ajax请求发送到handler.ashx。 我已经检查了ajax请求的所有就绪状态和状态。 在点击事件处理程序处理器.ashx被调用,但没有收到处理程序的响应。

当我删除条件“if(xhr.readystate == 4)”并仅保留“if(xhr.status == 200)”然后它正在工作...

请看下面的代码并告诉我出了什么问题???

感谢!!! :)

JS:

<script type="text/javascript">
    function setActive(element) 
    {

        element.style.color = '#ff0000';

        var links = document.getElementsByName("link");
        for (i = 0; i < 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 && 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;
    } 


    </script>

.aspx文件:

<body>
<form id="form1" runat="server">
<div>

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



</div>
<div>
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
    <div id="resText"></div>
    </asp:ContentPlaceHolder>
</div>
</form>
</body>

处理程序文件:      &lt;%@ WebHandler Language =“C#”Class =“Handler”%&gt;

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

}

0 个答案:

没有答案