javascript - XMLHttpRequest status returning 404 - Stack Overflow

I have some bit of code from the internet that updates my webpage when I type in a text input.My code i

I have some bit of code from the internet that updates my webpage when I type in a text input.

My code is below

function validate(field, query) {
    var xmlhttp;
    if (window.XMLHttpRequest) { // for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
            document.getElementById(field).innerHTML = "Validating..";
        } else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(field).innerHTML = xmlhttp.responseText;
        } else {
            document.getElementById(field).innerHTML = "Error Occurred. <a href='index.php'>Reload Or Try Again</a> the page.";
        }
    }
    xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false);
    xmlhttp.send();
}

After debugging I found out that the code is run twice (afaik). The first time xmlhttp.readyState is 1, meaning that the request is being set up. The second time it's 4 meaning it's plete. So this is working like intended.

The problem is that it always returns the Error Occurred bit in the field. The reason why is that xmlhttp.status keeps the status number 404, meaning that it is not found. I have no clue why it returns 404. If the browser I'm using is important, I'm using the latest version of Safari. I also checked the latest version of Chrome and got the same problem.

I have some bit of code from the internet that updates my webpage when I type in a text input.

My code is below

function validate(field, query) {
    var xmlhttp;
    if (window.XMLHttpRequest) { // for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
            document.getElementById(field).innerHTML = "Validating..";
        } else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(field).innerHTML = xmlhttp.responseText;
        } else {
            document.getElementById(field).innerHTML = "Error Occurred. <a href='index.php'>Reload Or Try Again</a> the page.";
        }
    }
    xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false);
    xmlhttp.send();
}

After debugging I found out that the code is run twice (afaik). The first time xmlhttp.readyState is 1, meaning that the request is being set up. The second time it's 4 meaning it's plete. So this is working like intended.

The problem is that it always returns the Error Occurred bit in the field. The reason why is that xmlhttp.status keeps the status number 404, meaning that it is not found. I have no clue why it returns 404. If the browser I'm using is important, I'm using the latest version of Safari. I also checked the latest version of Chrome and got the same problem.

Share asked Sep 14, 2014 at 6:55 KrowiKrowi 1,6253 gold badges21 silver badges40 bronze badges 4
  • see this link:forums.iis/t/… – Suchit kumar Commented Sep 14, 2014 at 7:16
  • Use the Network tab of Chrome's dev tools to see if your code is requesting the correct URL. – Seventoes Commented Sep 14, 2014 at 7:27
  • 2 @Seventoes, please post this as an answer if you want some rep and I can plete this question. I used the dev tools and found out that xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false); starts in the default directory and the file was in /scripts/validation.php. Adding /scripts/ in front of validation.php fixed it. – Krowi Commented Sep 14, 2014 at 8:24
  • Done! Chrome's dev tools are absolutely invaluable when doing any sort of web dev. It should be sitting there open at all times while developing :D – Seventoes Commented Sep 14, 2014 at 8:26
Add a ment  | 

1 Answer 1

Reset to default 0

Make sure that your code is actually requesting the correct URL. You can do this with most modern browser's developer tools, including the Network tab of Chrome's.

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

相关推荐

  • javascript - XMLHttpRequest status returning 404 - Stack Overflow

    I have some bit of code from the internet that updates my webpage when I type in a text input.My code i

    17小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信