XMLHttpRequest (Ajax) 错误 [英] XMLHttpRequest (Ajax) Error

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

问题描述

我在 JavaScript 中使用 XMLHttpRequest.但是,它给了我一个错误,我不知道我的问题是什么.
我必须解析一个 XML 文件并将其内容分配给网页 - 这是我的代码:

I'm using XMLHttpRequest in JavaScript. However, it gives me an error, and I don't know what my problem is.
I have to parse an XML file and assign its contents to the webpage - here's my code:

<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 += "
XMLHttprequest 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 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 编程,并且我在 Google Chrome 上运行我的网站.

Just for reference, I typically program in C# or Java, and I'm running my website on Google Chrome.

推荐答案

所以这里可能有一些问题.

So there might be a few things wrong here.

首先阅读如何使用 XMLHttpRequest.open() 因为还有第三个可选参数用于指定是否发出异步请求,默认为 true.这意味着您正在发出一个异步请求,并且需要在执行 send() 之前指定一个回调函数.以下是 MDN 的示例:

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

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 文件提供服务.

Second, since you're getting a 101 error, you might use the wrong URL. So make sure that the URL you're making the request with is correct. Also, make sure that your server is capable of serving your quiz.xml file.

您可能需要通过简化/缩小问题所在来进行调试.所以我会先做一个简单的同步请求,这样你就不必担心回调函数了.因此,这是 MDN 中的另一个用于发出同步请求的示例:

You'll probably have to debug by simplifying/narrowing down where the problem is. So I'd start by making an easy synchronous request so you don't have to worry about the callback function. 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,您可以参考 MDNJavascript API 文档/示例/教程.

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

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

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