首页 新闻 赞助 找找看

ASP.NET通过XMLHttpRequest实现ajax出问题了

0
悬赏园豆:30 [已解决问题] 解决于 2013-05-26 09:38

贴上所有的代码

我折腾了个把小时,实在不知道哪里错了,

这个代码原本是vs2005上的,我搬到vs2008上了,现在就是好像没有返回

Default.aspx

  1 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2 
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4 
  5 <html xmlns="http://www.w3.org/1999/xhtml">
  6 <head runat="server">
  7     <title>无标题页</title>
  8     
  9     <script type="text/javascript">
 10     //检查用户名是否存在
 11 function userCheck(){
 12     //获取用户输入的用户名
 13     var userName=$("TextBox1").value;
 14     alert("收到TextBox1的值"+userName);
 15     if(userName==""){
 16         $("showmsg").innerHTML="<p>用户名不能为空</p>";
 17         $("TextBox1").focus();
 18     }
 19     else{
 20         send_request('Default2.aspx?username='+userName);
 21     }
 22 }
 23 var http_request=false;
 24 function send_request(url){
 25     alert("接收到的url "+url);
 26     http_request=false;
 27     //如果是Mozilla浏览器
 28     if(window.XMLHttpRequest){
 29         http_request=new XMLHttpRequest();
 30         /*
 31             有些版本的Mozilla 浏览器处理服务器返回的未包含XML mime-type 头部信息的内容时会出错。
 32             因此,要确保返回的内容包含text/xml 信息
 33         */
 34         if(http_request.overrideMimeType){
 35             //设置MIME类别
 36             http_request.overrideMimeType('text/xml');
 37         }
 38     }
 39     else if(ActiveXObject){
 40         /*
 41             IE浏览器
 42             创建方法有两种 为了确保创建
 43             试用两种
 44         */
 45         try{
 46             http_request=new ActiveXObject("Msxml2.XMLHTTP");
 47         }
 48         catch(e){
 49             try{
 50                 http_request=new ActiveXObject("Microsoft.XMLHTTP");
 51             }
 52             catch(e)
 53             {}
 54         }
 55     }
 56     if(!http_request){
 57         //如果创建失败
 58         window.alert("can't creat XMLHttpRequest");
 59         return false;
 60     }
 61     //当服务器返回信息时客户端的处理方式
 62     //将相应的处理函数名称赋给XMLHttpRequest 对象的onreadystatechange 属性
 63     http_request.onreadystatechange=processRequest;
 64     //建立对服务器的调用
 65     //如果为真,当状态改变时会调用onreadystatechange属性指定的回调函数
 66     http_request.open("GET",url,true);
 67     http_request.send(null);
 68 }
 69 //回调函数
 70 function processRequest(){
 71 
 72 /*
 73     0 (未初始化)
 74     对象已建立,但是尚未初始化(尚未调用open方法)
 75     1 (初始化)
 76     对象已建立,尚未调用send方法
 77     2 (发送数据)
 78     send方法已调用,但是当前的状态及http头未知
 79     3 (数据传送中)
 80     已接收部分数据,因为响应及http头不全,这时通过responseBody和responseText获取部分数据会出现错误,
 81     4(完成)
 82     数据接收完毕,此时可以通过通过responseBody和responseText获取完整的回应数据
 83 */
 84     if(http_request.readyState==4){
 85             showMsg("接收一次看看");
 86         //判断对象状态
 87         if(http_request.status==200){
 88             //请求已成功,请求所希望的响应头或数据体将随此响应返回
 89             var str=http_request.responseText;
 90             showMsg(str);
 91         }
 92         else{
 93             alert("the request-page abnormal");
 94         }
 95     }
 96 }
 97 //处理返回信息
 98 function showMsg(str){
 99     if(str=="1"){
100         $('showmsg').innerHTML="jieshou";
101     }
102     else if(str=="2"){
103         $('showmsg').innerHTML="已存在";
104     }
105     else if(str=="3"){
106         $('showmsg').innerHTML="用户名不能为空";
107     }
108 }
109 function $(id){
110     return document.getElementById(id);
111 }
112 </script>
113 </head>
114 <body>
115     <form id="form1" runat="server">
116     <div>
117      <asp:Label ID="Label1" runat="server" Text="Label">用户账号</asp:Label>
118         <asp:TextBox ID="TextBox1" runat="server" onblur="userCheck()"></asp:TextBox><span id="showmsg"></span><br />
119          <asp:Label ID="Label2" runat="server" Text="Label">用户密码</asp:Label>
120         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
121         <asp:Button ID="Button1" runat="server" Text="确认注册" />
122     </div>
123     </form>
124 </body>
125 </html>
View Code

Default2.aspx

 1 using System;
 2 using System.Collections;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Security;
 8 using System.Web.UI;
 9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 
14 public partial class Default2 : System.Web.UI.Page
15 {
16     protected void Page_Load(object sender, EventArgs e)
17     {
18         string userName = Request.QueryString["username"];
19         Response.Write("接收到");
20         userName = userName.ToLower();
21         if (userName.Equals(""))
22         {
23             Response.Write("3");
24             return;
25         }
26         else if (userName.Equals("sun"))
27         {
28             Response.Write("2");
29             return;
30         }
31         else
32         {
33             Response.Write("1");
34             return;
35         }
36     }
37 }
View Code

希望大家能帮解决下

Sun_LuoFlow的主页 Sun_LuoFlow | 初学一级 | 园豆:161
提问于:2013-05-25 22:19
< >
分享
最佳答案
0

只有调试步骤  1 看看服务端Id 你取的对不对 2 换浏览器调试 3 这种ajax调用方式太落后了吧,直接用  $.post  $.get 多好?

收获园豆:30
dreamhappy | 初学一级 |园豆:4 | 2013-05-26 00:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册