XMLHTTP请求状态始终返回0

编程入门 行业动态 更新时间:2024-10-28 20:26:30
本文介绍了XMLHTTP请求状态始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我有一页要检查用户的可用性,我已经通过asp中的XML Http请求进行了此操作,问题是状态对象始终返回0 我的代码是:

Hello everyone, I have one page in which i check availability of users , i have made this through XML Http request in asp, the problem is that the status object always returns 0 my code is :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml"> <head runat="server"> <title></title> <script type = "text/javascript"> var xmlhttp; function CheckUserNameAvailability() { xmlhttp = null; // if (username = "" || username.length < 4) { // document.getElementById("<%=lblMsg.ClientID%>").innerHTML = ""; // return false; // } if (window.XMLHttpRequest) { // code for all new browsers xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE5 and IE6xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp != null) { alert("AjaxPageCalled"); var username = document.getElementById("txtUserName").value; alert(username); xmlhttp.onreadystatechange = state_Change; xmlhttp.open("GET", "AjaxEnginePage.aspx?UserName=" + username, true); xmlhttp.send(null); } else { alert("Oops..Your browser does not support XMLHTTP."); } } function state_Change() { // alert("state_chage"); //alert(xmlhttp.readyState); //alert(xmlhttp.status); if (xmlhttp.readyState == 4) { // 4 = "loaded" if (xmlhttp.status == 200) { //request was successful //check if username is available and message var lblMesg = document.getElementById("<%=lblMsg.ClientID%>"); if (xmlhttp.responseText == "True") { lblMesg.style.color = "Green"; lblMesg.innerHTML = "Username available."; } else { lblMesg.innerHTML = "Username already in use."; lblMesg.style.color = "Red"; } } else { alert("Oops..username availability could not be checked."); } } } </script> </head> <body> <form id="form1" runat="server"> <div> <table cellpadding="0" cellspacing="2"> <tr> <td>User Name:</td> <td> <asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox> <asp:Label ID="lblMsg" runat="server"></asp:Label> </td> </tr> <tr> <td>Email:</td> <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td> </tr> <tr> <td></td> <td><asp:Button ID="btnRegister" runat="server" Text="Register" OnClientClick ="CheckUserNameAvailability();" /></td> </tr> </table> </div> </form> </body> </html> Ajax Engine Page.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; public partial class AjaxEnginePage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CheckUserNameAvailability(); } public void CheckUserNameAvailability() { if (Request["UserName"] != null) { string username = Request["UserName"].ToString (); string result = "False"; System.Web.Security.MembershipUser user = Membership.GetUser(username); if (user != null) { result = "False"; } else { result = "True"; } Response.Clear(); Response.Write(result); Response.End(); } } }

请帮忙.. 问候, Krunal

Please help .. Regards, Krunal

推荐答案

将jscript移到Head部分 Move your jscript above Head section

更多推荐

XMLHTTP请求状态始终返回0

本文发布于:2023-10-28 02:32:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   XMLHTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!