日期:2014-05-19  浏览次数:20603 次

.NET中AJAX如何解决接收返回值是整个页面
刚刚接触AJAX,下面是小D的代码,在AJAX接收返回值时,alert回返的却是整个页面,把html,head,body中的所有东西全返回了,现在只想接收Response.write里面的东西怎么办!
JS:


      var   xmlHttpm   =   null;
function   createXMLHttpRequest()
{
        if   (window.ActiveXObject)  
{
                xmlHttp   =   new   ActiveXObject( "Microsoft.XMLHTTP ");
        }  
        else   if   (window.XMLHttpRequest)
{
                xmlHttp   =   new   XMLHttpRequest();
        }
}

function   checkText()
{
        createXMLHttpRequest();
       
        xmlHttp.onreadystatechange=handleStateChange;
        xmlHttp.open( "GET ", "?UserName= '1111 ' ",false);
        xmlHttp.send(null);
}

function   handleStateChange()  
{
        if(xmlHttp.readyState   ==   4)
{
                if(xmlHttp.status   ==   200)
{
          document.write(request.responseText);      
                }
        }
}

.ASPX页面

<body>
    <form   id= "Form1 "   method= "post "   runat= "server ">  
          <INPUT   type= "button "   value= "Button "   onclick= "checkText(); ">
    </form>
</body>

.NET

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//Button1.Attributes.Add( "onclick ", "checkText() ");
string   userName   =   " ";
if   (Request.QueryString[ "UserName "]   !=   null)
                {
        userName   =   Request.QueryString[ "UserName "].ToString();
        Response.Write( "您输入的是 "+userName);
     
                }
}




------解决方案--------------------
innerHtml
------解决方案--------------------
后台自己可以控制的情况下,在pageload中这样写
Response.Clear();
Response.Write(...);
Response.Close;
这样就只输出自己想要的内容

如果不能控制就只能在前台用正则表达式过滤自己所需要的内容
------解决方案--------------------
上。
------解决方案--------------------
在Response.Write()后面加上Response.End()
------解决方案--------------------
这个很简单啊,你想返回什么就Response.Write( "返回值 ");
就可以了啊 ,