Javascript Ajax XHTTP请求无法工作
这件事我已经耽搁了一段时间了,请帮帮我 我正在尝试执行XHTTP请求,解析?这是XML文档的正确术语吗。请求将不会输入myFunctionxml,我不知道为什么,它正在响应200 ok,所以应该可以吧 请求应该检查XML中HTML表单中输入的电子邮件,如果找到,则相应地作出响应 谢谢你的帮助 Javascript如下:Javascript Ajax XHTTP请求无法工作,javascript,ajax,xml,Javascript,Ajax,Xml,这件事我已经耽搁了一段时间了,请帮帮我 我正在尝试执行XHTTP请求,解析?这是XML文档的正确术语吗。请求将不会输入myFunctionxml,我不知道为什么,它正在响应200 ok,所以应该可以吧 请求应该检查XML中HTML表单中输入的电子邮件,如果找到,则相应地作出响应 谢谢你的帮助 Javascript如下: <script type="text/javascript"> function loadDoc() { email = document.getElementBy
<script type="text/javascript">
function loadDoc()
{
email = document.getElementById('email');
password = document.getElementById('password');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
myFunction(xhttp);
}
}
}
xhttp.open("POST", "customer.xml", true);
xhttp.send();
function myFunction(xml)
{
var x, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("email");
for (i = 0; i < x.length; i++)
{
if (x[i].nodeValue == document.getElementById('email'))
{
return (true);
alert('Success, you have logged in!');
}
else
{
alert('Failed to log in');
document.myForm.email.focus();
return false;
}
}
}
</script>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<custid />
<firstname />
<lastname />
<email />
<password />
<phone />
</customer>
<customer>
<custid>562b5d237b599</custid>
<firstname>1</firstname>
<lastname>1</lastname>
<email>1</email>
<password>1</password>
<phone>1</phone>
</customer>
<customer>
<custid>562b62824e3f7</custid>
<firstname>ben</firstname>
<lastname>1</lastname>
<email>1</email>
<password>1</password>
<phone>1</phone>
</customer>
<customer>
<custid>562b63224b80f</custid>
<firstname>ben</firstname>
<lastname>ben</lastname>
<email>ben@gmail.com</email>
<password>ben</password>
<phone>0266746374</phone>
</customer>
<customer>
<custid>562b68ea06ed1</custid>
<firstname>mark</firstname>
<lastname>mark</lastname>
<email>mark@gmail.com</email>
<password>mark</password>
<phone />
</customer>
</customers>
在这里,您尝试在创建xhttp的函数之外使用它。注意将}放在何处。如果对代码缩进的方式更加小心,则会使这一点更加明显
这永远不会是真的。getElementById将返回HTML元素节点,而不是字符串。大概你在寻找。价值
调试代码时,需要检查正在测试的值,以查看它们是否实际匹配
在这里,如果第一个节点值不匹配,那么您将立即返回,这样您就不会测试任何其他节点
您需要将失败的条件放在循环之外,这样只有在没有匹配项时才会触发
当然,这个系统是完全不安全的,因为任何人都可以请求XML文件并读取其中的密码。您能为您的代码发布一个fiddle链接吗?想看看你的customer.xml。并检查开发人员控制台的网络选项卡中显示的响应。是否要传递xhttp.responseText提供的XML数据?请参阅以了解如何使用XMLHttpRequestAPI。您还需要将检索到的文本序列化为XML。我不知道如何使用fiddle,所以我在CSS区域发布了XML。。。我也会把它编辑到上面。对不起,我没有正确阅读你的答案,谢谢你,效果很好。但我无法使它与电子邮件和XML相匹配。这个代码正确吗?对于i=0;i
}
xhttp.open("POST", "customer.xml", true);
xhttp.send();
x[i].nodeValue == document.getElementById('email')
else
{
alert('Failed to log in');
document.myForm.email.focus();
return false;
}