嘿我试图在同一页面中运行两个ajax函数
将一个值发送到用户名 另一个发送放入电子邮件的值,发送给ajax.php& ajax1.php
下面是我的代码:
<script type="text/javascript">
function AjaxFunction(username)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck()
{
if(httpxml.readyState==4)
{
document.getElementById("msg").innerHTML=httpxml.responseText;
}
}
httpxml.onreadystatechange=stateck;
url="ajax.php?value=" + username.value;
httpxml.open("GET",url,true);
httpxml.send(null);
}
function AjaxFunction(email)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck()
{
if(httpxml.readyState==4)
{
document.getElementById("msg1").innerHTML=httpxml.responseText;
}
}
httpxml.onreadystatechange=stateck;
url1="ajax.php1?value=" + email.value;
httpxml.open("GET",url1,true);
httpxml.send(null);
}
</script>
<form id="form" name="form" method="post" action="">
<p>
<label>Username
<input type="text" name="username" id="username" onBlur="AjaxFunction(username);">
</label>
<div id="msg"></div>
<p>Email
<label>
<input type="text" name="email" id="email" onBlur="email(EMAIL);">
</label>
<div id="msg1"></div>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</form>
然后应将响应文本放入div标签(msg&amp; msg1)
由于
答案 0 :(得分:1)
您是否查看了jQuery,它将大大降低页面的复杂性,并为浏览器抽象提供一个很好的API。我已经可以看到跨浏览器问题了。
答案 1 :(得分:0)
你应该添加到“if(httpxml.readyState == 4)”的另一个if语句是“if(httpxml.status == 200)”。这确保服务器端没有任何问题。同时调用这两个请求是危险的,因为对一个请求所做的任何更改都可能会覆盖另一个。
答案 2 :(得分:0)
您希望避免重复代码?创建第三个函数,创建httpxml对象并将其传递给前两个。
httpxml = createAjax();
AjaxFunction(httpxml, username);