简体   繁体   English

javascript xmlhttprequest无法正常工作

[英]javascript xmlhttprequest won't work

I'm learning javascript for a new project. 我正在为一个新项目学习javascript。 I have seen many tutorials about javascript and xmlhttprequest, but when I try to run decode it won't work. 我看过许多关于javascript和xmlhttprequest的教程,但是当我尝试运行解码时,它将无法正常工作。 can somebody help me. 有人可以帮我吗

here is my html+javascript code: 这是我的html + javascript代码:

 <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>xml reader</title> </head> <body> <script type="text/javascript"> var xmlhttp; var xmldoc; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=stateChange(); xmlhttp.open("GET","info.xml",true); xmlhttp.send(null); xmldoc = xmlhttp.responceXML.documentElement; document.write(xmlhttp.getElementsByTagName("vakken")[0].childNodes[0].nodeValue); </script> </body> </html> 

here is my xml file: 这是我的xml文件:

 <?xml version="1.0" encoding="utf-8"?> <info> <name>test</name> <nummber>10</nummber> </info> 

You have a ReferenceError in your JavaScript that prevents it from executing completely. 您的JavaScript中有一个ReferenceError,它阻止它完全执行。 "stateChange" is undefined. “ stateChange”未定义。 You need to define the stateChange function. 您需要定义stateChange函数。

Also it looks like "response" is spelled wrong (responce). 而且看起来“响应”的拼写错误(响应)。

Please go through this tutorial from W3Schools: http://www.w3schools.com/XML/xml_http.asp 请从W3Schools阅读本教程: http : //www.w3schools.com/XML/xml_http.asp

You're very close so use W3Schools as a resource to fix up some other problems in your code. 您非常接近,因此可以使用W3Schools作为资源来修复代码中的其他问题。

maybe you should use xmlhttp.onreadystatechange and set it to a real function. 也许您应该使用xmlhttp.onreadystatechange并将其设置为实函数。 the function stateChange() does not exist in the above example. 上面的示例中不存在stateChange()函数。

a good working example that does almost the same thing as you do is here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first 一个与您做的几乎相同的工作示例,在这里: http : //www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

stateChange() that you call onreadystatechange is not defined - add a function named stateChange that will be called once the response returns. 未定义您在onreadystatechange上调用的stateChange() -添加一个名为stateChange的函数,一旦响应返回,该函数将被调用。

An example of such a function: 此类功能的示例:

function stateChange() {
    if(xmlhttp.readyState == 4){
        xmldoc = xmlhttp.responseXML.documentElement;
        document.write(xmlhttp.getElementsByTagName("vakken")[0].childNodes[0].nodeValue);
    }
}

You must remember that AJAX is asynchronous - the response must be handled separately at the time it returns - hence the readystatechanged event usage... 您必须记住AJAX是异步的-响应返回时必须单独处理-因此readystatechanged事件用法...
Remove the two lines after sending the request from your original code (they are in the stateChange function). 从原始代码发送请求后删除两行(它们在stateChange函数中)。

Are you don't receiving an answer in the Request or may it be the case, that you get an answer but it's not written into your dom? 您是否没有在请求中收到答案,或者是这样,您得到了答案但没有写到您的dom中? Lately I had the problem, that document.write wouldn't work in one of my pages. 最近,我遇到了问题,那个document.write在我的其中一个页面中不起作用。 I found out, that document.write is not allowed in XHTML-based pages (My page was XHTML 1.1, yours is XHTML 1.0). 我发现,在基于XHTML的页面中不允许document.write (我的页面是XHTML 1.1,您的页面是XHTML 1.0)。 So this is just a guess. 所以这只是一个猜测。 You could test this with the firebug-extension for firefox. 您可以使用firebug扩展的firebug进行测试。 Althought all common browsers respect, that document.write is not allowed and throw a error-message, IE does however support document.write in any case... 尽管所有常见浏览器都尊重该document.write ,但不允许该document.write并引发错误消息,但是IE在任何情况下均支持document.write ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM