XMLHTTP请求状态始终返回0 [英] XMLHTTP Request status always returning 0
本文介绍了XMLHTTP请求状态始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一页要检查用户的可用性,我已经通过asp.net中的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.net, 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文