XMLHttprequest.send(null)正在崩溃我的代码

时间:2022-09-15 18:38:42

I'm currently writing a search function using JavaScript.

我目前正在使用JavaScript编写搜索功能。

However, when I attempt to test my creation, I find that it stops about halfway through for no discernible reason.

然而,当我试图测试我的创作时,我发现它已经中途停止,没有明显的原因。

Below is my code:

以下是我的代码:

document.getElementById("test").innerHTML = "";
var Connect = new XMLHttpRequest();
Connect.open("GET", "xmlTest.xml", false);
document.getElementById("test").innerHTML = "1";
Connect.send(null);
document.getElementById("test").innerHTML = "2";
var docX = Connect.responseXML;
var linjer = docX.getElementsByTagName("linjer");

The first line is there to clear a potential error message from earlier in the code. Then I attempt to open up an XML file, as I need to read from it.

第一行是清除代码中较早的潜在错误消息。然后我尝试打开一个XML文件,因为我需要从中读取它。

As you can see, I've entered two debug statements there; they will print 1 or 2 depending on how far I get in the code.

如您所见,我在那里输入了两个调试语句;它们将打印1或2,具体取决于我在代码中的距离。

Using this, I've found that it stops exactly on the Connect.send(null); statement (as 1 gets printed, but 2 never does), but I can't figure out why. Google says that it might be that chrome can't access local files, but when I found a way to allow Chrome to do this, it still did not work.

使用这个,我发现它完全停在Connect.send(null)上;声明(1打印,但2打印),但我无法弄清楚原因。 Google表示,Chrome可能无法访问本地文件,但当我找到一种方法允许Chrome执行此操作时,它仍然无效。

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


1  

This might be a synchronous issue that requires a response that your code simply is not getting.

这可能是一个同步问题,需要您的代码无法获得的响应。

Try using an async call instead:

请尝试使用异步调用:

Connect.open("GET", "xmlTest.xml", true);

Also make sure to setup proper callbacks since you'll be using async here now instead of synchronous code, like so:

还要确保设置正确的回调,因为您现在将使用异步而不是同步代码,如下所示:

// Global variable scope
var docX;
var linjer;

// Define your get function
getDoc = function(url, cbFunc) {

    var Connect = new XMLHttpRequest();

    // Perform actions after request is sent
    // You'll insert your callback here
    Connect.onreadystatechange = function() {
        // 4 means request finished and response is ready
        if ( Connect.readyState == 4 ) {
            // Here is where you do the callback
            cbFunc(Connect.responseXML);
        } 
    }; 

    // 'true' param means async, it is also the default
    Connect.open('GET', url, true);
    Connect.send();
}

// Define your callback function    
callbackFunction = function(responseXML) {
    // XML file can now be stored in the global variable
    window.docX = responseXML;
    window.linjer = window.docX.getElementsByTagName("linjer");
}

// And here is the call you make to do this
getDoc("xmlTest.xml", callbackFunction); 

For better understanding of all of this, do some research on scope, closures, callbacks, and async.

为了更好地理解所有这些,请对范围,闭包,回调和异步进行一些研究。

#1


1  

This might be a synchronous issue that requires a response that your code simply is not getting.

这可能是一个同步问题,需要您的代码无法获得的响应。

Try using an async call instead:

请尝试使用异步调用:

Connect.open("GET", "xmlTest.xml", true);

Also make sure to setup proper callbacks since you'll be using async here now instead of synchronous code, like so:

还要确保设置正确的回调,因为您现在将使用异步而不是同步代码,如下所示:

// Global variable scope
var docX;
var linjer;

// Define your get function
getDoc = function(url, cbFunc) {

    var Connect = new XMLHttpRequest();

    // Perform actions after request is sent
    // You'll insert your callback here
    Connect.onreadystatechange = function() {
        // 4 means request finished and response is ready
        if ( Connect.readyState == 4 ) {
            // Here is where you do the callback
            cbFunc(Connect.responseXML);
        } 
    }; 

    // 'true' param means async, it is also the default
    Connect.open('GET', url, true);
    Connect.send();
}

// Define your callback function    
callbackFunction = function(responseXML) {
    // XML file can now be stored in the global variable
    window.docX = responseXML;
    window.linjer = window.docX.getElementsByTagName("linjer");
}

// And here is the call you make to do this
getDoc("xmlTest.xml", callbackFunction); 

For better understanding of all of this, do some research on scope, closures, callbacks, and async.

为了更好地理解所有这些,请对范围,闭包,回调和异步进行一些研究。