<input type="button"> XMLHttpRequest 的不同行为与&lt;按钮&gt; [英] Different Behavior of XMLHttpRequest for &lt;input type=&quot;button&quot;&gt; vs. &lt;button&gt;

查看:28
本文介绍了<input type="button"> XMLHttpRequest 的不同行为与&lt;按钮&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <script type="text/javascript" src="script.js"></script>
    </head>

    <body>
        <form>
            <button id="getInfoButton1"></button>
            <input type="button" id="getInfoButton2"></input>
        </form>
    </body>
</html>

随附的 JavaScript 文件:

With the accompanying JavaScript file:

script.js

window.onload = initAll;
var req;

function initAll()
{
    document.getElementById("getInfoButton1").onclick = getInfo;
    document.getElementById("getInfoButton2").onclick = getInfo;
}

function getInfo()
{
    req = new XMLHttpRequest();
    var URL = "index.html";
    req.open("GET", URL, true);
    req.onreadystatechange = whatsTheStatus;
    req.send(null);
}

function whatsTheStatus()
{
    if (req.readyState != 4) { return; }
    alert(req.status);
}

(我已经削减了很多代码,但这个例子仍然突出了错误)

(I've pared down my code a lot, but this example still highlights the error)

问题是这样的:当您加载它并单击两个按钮时,第一个将显示 0 的状态,而第二个将显示 200 的状态.

The question is this: When you load this, and click both buttons, the first one will display a status of 0, while the second one displays a status of 200.

当然,我希望两者都显示 200,但我不知道为什么

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