首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ASP.NET ajax responseText回来的却是网页的HTML代码,求解

2013-01-02 
ASP.NET ajax responseText返回的却是网页的HTML代码,求解如题,我用一个aspx 页面写js请求一个一般处理程

ASP.NET ajax responseText返回的却是网页的HTML代码,求解
如题,
我用一个aspx 页面写js请求一个一般处理程序
然而,给我返回的却是是aspx整个页面的代码,
我在一般处理程序里面设置断点, 竟然没有跳过去
这是不是能说明,他请求没成功。
但是,ajax返回来的那两个状态却是成功的
readystate==4
staus==200
这是为何呢,
求高手解答!!
一下是代码


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test11.aspx.cs" Inherits="Test11" %>

<%--<%@ Register src="control/TestCity.ascx" tagname="TestCity" tagprefix="uc1" %>--%>

<!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">
    <title></title>
<%--  <script src="js/ajax.js" type="text/javascript"></script>--%>
    <script type="text/javascript">

        var xmlHttp = null;
        function loadXmlHttp() {
            if (window.XMLHttpRequest) { // IE7, Mozilla, Safari, Opera, etc.
                xmlHttp = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE 5.x and 6
                }
                catch (e) {

                    alert(e);
                }
            }
        }

        function sendRequest() {
            // alert(url);
            loadXmlHttp();
          //  alert(xmlHttp);
            if (xmlHttp) {

                // Open HTTP connection to url.
                xmlHttp.open("GET", "ChangeCity.ashx", true); // true = async

                // Define the callback function for async call
                xmlHttp.onreadystatechange = onCallback;

                // Specify form data in request body
                xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');



                // Send request without any additional parameters
                xmlHttp.send(null);
            }
        }

        function onCallback() {
            // Check for the completed status 
            if (xmlHttp.readyState == 4) {
                // Check for successful server response
                if (xmlHttp.status == 200) {
                    alert(xmlHttp.responseText);
                }
                else {
                    // HTTP error
                    alert('Error: ' + xmlHttp.status);
                }

            }
        }
    
</script> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <%-- <asp:Button runat="server" ID="bb1" Text="测试" OnClientClick="  sendRequest()" />--%>
        <input type="button" value="Cheshi" onclick="sendRequest()" />
<%--        <uc1:TestCity ID="TestCity1" runat="server" />--%>
<input type="text" id="txt1" />
<select id="s1"></select>
    </div>
    </form>
</body>
</html>



<%@ WebHandler Language="C#" Class="ChangeCity" %>

using System;
using System.Web;
using System.Web.Services;



/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ChangeCity : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //context.Response.Write("javascript:alert(1111)");
       context.Response.Write("0000000000000");
      // context.Response.End();
    }


    //public void GetDate(HttpContext context)
    //{
    //    string ss = context.Request["id"].ToString();
    //   // string[] aa = { };
    //    context.Response.Write(ss);
    //}
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}


[解决办法]
 context.Response.Write("0000000000000");
 context.Response.Flush();
 context.Response.End();
[解决办法]
xmlHttp.open("GET", "ChangeCity.ashx", true); // true = async

既然请求的是ahsx页面,不会输出html代码什么的,只可能输出ashx用response对象输出的内容。自己再好好检查下
[解决办法]
I think 


context.Response.End();


is needed.
[解决办法]
你没有用Response.Write()写入数据,所以返回html,你写点数据然后alert下,就是你写的数据了。

热点排行