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.
- 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 ofvalidation.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
1 Answer
Reset to default 0Make 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
评论列表(0条)