javascript - XMLHttpRequest connection check - Stack Overflow

I am trying to write a streamlined version of a XMLHttpRequest demo script shown here:.asp?filename=try

I am trying to write a streamlined version of a XMLHttpRequest demo script shown here:

.asp?filename=tryajax_first

I'm only going to use this on iPad, so I don't have to check for older versions of IE, and so on. On button click, I want to check if the connection exists. Here's my entire html page, including JavaScript snippet:

<html>
<head>
<script>
var myURL = "";

function testConnection(url) {
    var xmlhttp;

    xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert("Connected!");
        } else {
            alert("Not connected!");
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
</script>
</head>
<body>


<button type="button" onclick="testConnection(myURL)">Test Connection</button>

</body>
</html>

For some weird reason, even though I'm online, when I click the button, I get repeated "Not connected" alerts, and only after a while I get the "Connected" alert, followed by no alerts.

Looks like I messed up, but I can't see where. What should I change to make it work?

I am trying to write a streamlined version of a XMLHttpRequest demo script shown here:

http://www.w3schools./ajax/tryit.asp?filename=tryajax_first

I'm only going to use this on iPad, so I don't have to check for older versions of IE, and so on. On button click, I want to check if the connection exists. Here's my entire html page, including JavaScript snippet:

<html>
<head>
<script>
var myURL = "http://www.google.";

function testConnection(url) {
    var xmlhttp;

    xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert("Connected!");
        } else {
            alert("Not connected!");
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
</script>
</head>
<body>


<button type="button" onclick="testConnection(myURL)">Test Connection</button>

</body>
</html>

For some weird reason, even though I'm online, when I click the button, I get repeated "Not connected" alerts, and only after a while I get the "Connected" alert, followed by no alerts.

Looks like I messed up, but I can't see where. What should I change to make it work?

Share Improve this question edited Jun 27, 2013 at 16:00 Givi 1,7342 gold badges21 silver badges35 bronze badges asked Jun 27, 2013 at 15:53 Dimitri VorontzovDimitri Vorontzov 8,11412 gold badges51 silver badges78 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

If you can use xhr2, you can learn stuff from this tutorial and rewrite your code to something like this:

function testConnection(url) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onload = function() { alert("Connected!"); }
    xmlhttp.onerror = function() { alert("Not Connected"); }
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

If you send request to another domain, you may get error even if it exists, if the target server has Same-Domain-Policy restriction (default). If the target server is on another domain, it must send header

Access-Control-Allow-Origin: *

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745384182a4625365.html

相关推荐

  • javascript - XMLHttpRequest connection check - Stack Overflow

    I am trying to write a streamlined version of a XMLHttpRequest demo script shown here:.asp?filename=try

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信