Ajax加载信息

asp.net页面:

<head runat="server">
    <title></title>
    <script src="JavaScript/JScript.js" type="text/javascript"></script>
</head>
<body onload ="Ajax()">
    <form id="form1" runat="server">
    <img src="11.gif" id="img" />
    <div id="myDiv">
    </div>
    </form>
</body>

----------------------------------------------------------------------------------

js脚本:

var xmlhttp;
function loadXMLDoc() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function Ajax() {
    loadXMLDoc();
    xmlhttp.onreadystatechange = star;
    xmlhttp.open("GET", "ashx/Handler.ashx", true);
    xmlhttp.send();
}

function star() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {  //如果状态码为200则表示处理成功;
            document.getElementById("img").style.display = "none";
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
        else {
            document.getElementById("img").style.display = "none";
            document.getElementById("myDiv").innerHTML = "Ajax返回错误!";
        }
    }
}

-------------------------------------------------------------

ashx:

string sql = "select rows from sysindexes where id = object_id('Ip') and indid in (0,1) ";
          
        SqlCommand cmd = new SqlCommand();
        using (SqlConnection con = new SqlConnection(SqlHelper.SQLConnection))
        {
            con.Open();
            object myobj = SqlHelper.ExecuteScalar(con, System.Data.CommandType.Text, sql, null);
            return myobj.ToString ();
        }

posted @ 2011-05-26 09:44  Silence-01  阅读(474)  评论(0)    收藏  举报