XMLHtt prequest(阿贾克斯)错误 [英] XMLHttpRequest (Ajax) Error

查看:219
本文介绍了XMLHtt prequest(阿贾克斯)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript时,我有一个xmlhtt prequest错误。我有一个XML文件,我想分析和分配给它的内容的网页。我已经做了这部分我的研究,我似乎无法找到答案。

I have an xmlhttprequest error when working with javascript. I have an XML file which I would like to parse and assign it's contents to the webpage. I have done my research on this part, and I cannot seem to figure it out.

<script = "text/javascript">
    window.onload = onPageLoad();
    var questionNum = 0;
    function onPageLoad(questionNum){

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","quiz.xml");

        try{
            xmlhttp.send(null);//here a xmlhttprequestexception number 101 is thrown 
        }catch(err){
            document.getElementById("body").innerHTML += "\nXMLHttprequest error: " + err.description;
        //this prints "XMLHttprequest error: undefined" in the body.
        }

        xmlDoc=xmlhttp.responseXML;
        parser=new DOMParser(); //this code is untested as it does not run this far.
    }
</script>

我的XML文件是简单的和相同的目录中。

My XML file is simple and inside the same directory.

<question>
    <query>what is 2+2?</query>
    <option>4</option>
    <option>5</option>
    <option>3</option>
    <answer>4</answer>
</question>

我想不出什么我的问题是。仅供参考我通常程序C#或Java,而我使用谷歌浏览器。

I can't figure out what my problem is. For reference I typically program in C# or Java, and I am using google chrome.

推荐答案

所以,可能有几件事错在这里。

So there might be a few things wrong here.

首先开始阅读如何使用 XMLHtt prequest.open() 因为没有指定是否进行异步请求,默认设置为true第三个参数。这意味着你正在做一个异步请求,并需要指定一个回调函数,在此之前的发送()。下面是从Mozilla开发者网络的例如:

First start by reading how use XMLHttpRequest.open() because there's a third optional parameter for specifying whether to make an asynchronous request, defaulting to true. This means you're making an asynchronous request and need to specify a callback function before you do the send(). Here's an example from the Mozilla Developer Network:

var oXHR = new XMLHttpRequest();

oXHR.open("GET", "http://www.mozilla.org/", true);

oXHR.onreadystatechange = function (oEvent) {  
    if (oXHR.readyState === 4) {  
        if (oXHR.status === 200) {  
          console.log(oXHR.responseText)  
        } else {  
           console.log("Error", oXHR.statusText);  
        }  
    }  
}; 

oXHR.send(null);  

其次,因为你得到一个101错误,你可能有错误的URL。因此,请确保您提出请求的URL是正确的。另外,还要确保你的服务器能够提供你的 quiz.xml 文件。

您可能得通过简化/缩小问题的所在调试。所以我开始我做一个简单的同步请求,所以你不必在担心回调函数的东西。因此,这里的来自MDN另一个例子做一个同步请求:

You'll probably have to debug by simplifying/narrowing down where the problem is. So I'd start my making an easy synchronous request so you don't have you worry about the "callback function" stuff. So here's another example from MDN for making a synchronous request:

var request = new XMLHttpRequest();  
request.open('GET', 'file:///home/user/file.json', false);   
request.send(null);  

if (request.status == 0)  
    console.log(request.responseText); 

另外,如果你是刚开始使用Javascript,您可以参考 MDN 了解使用Javascript API文档/例子/教程。

Also, if you're just starting out with Javascript, you could refer to MDN for Javascript API documentation/examples/tutorials.

这篇关于XMLHtt prequest(阿贾克斯)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆