xmlHttp.responseText为空

时间:2011-03-14 12:00:44

标签: javascript .net ajax xmlhttprequest

xmlHttp.responseText是空的,有什么问题和解决方案?

default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="xmlHttpOrnek._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script type="text/javascript">
var xmlHttp;
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
//--------------------------
function callAjaxFunc(val) {
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }

        //you can provide your page URL
        var url = "Default.aspx";
        url = url + "?kod=" + val;

        //state change event-this will occur ass soon as response comes from the url
        xmlHttp.onreadystatechange = stateChanged;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
}

function stateChanged() {

    if (xmlHttp.readyState == 4) 
    { //Display contents 
        var xmlResponse = xmlHttp.responseText;

        if (xmlResponse != '') {
            var inputMessage = document.getElementById('txb');
            inputMessage.value = xmlResponse.toString(); 
            alert(xmlResponse);
        }                    
    }
}
</script>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox ID="txb" runat="server"></asp:textbox>
        <asp:Button ID="btn" runat="server" Text="bas" OnClientClick="callAjaxFunc('y')" />
    </form>
</body>
</html>

default.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace xmlHttpOrnek
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string d = Request.QueryString["kod"];

            if (Request.QueryString["kod"] == "y")
            {
                Response.Write("Başarılı!");
                Response.End();
            }            
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我用这个副本粘贴创建了另一个项目;一切都一样,但第二个工作。我很惊讶。