Javascript Xmlhttprequest 404 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business xmlhttprequest 404 handling Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation xmlhttprequest returns 404 Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like javascript xmlhttprequest catch 404 you, helping each other. Join them; it only takes a minute: Sign up XMLHttpRequest status returning 404 up vote 0 down vote favorite I have some bit of code from the internet that updates my webpage when how to handle 404 error in javascript 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 {
Xhr Status 404
document.getElementById(field).innerHTML = "Error Occurred. Reload Or Try Again 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 complete. 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. javascript ajax xmlhttprequest share|improve this question asked Sep 14 '14 at 6:55 Krowi 5301618 see this link:forums.iis.net/t/… –Suchit Sep 14 '14 at 7:16 Use the Network tab of Chrome's dev tools to see if your code is requesting the correct URL. –Seventoes Sep 14 '14 at 7:27 1 @Seventoes, please post this as an answer if you want some rep and I can complete 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 dire
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Xhr Returned Response Code 404
and policies of this site About Us Learn more about Stack Overflow jquery ajax 404 error handling the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation catch 404 error javascript Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it http://stackoverflow.com/questions/25830847/xmlhttprequest-status-returning-404 only takes a minute: Sign up XMLHttpRequest detecting 404 (Not Found) up vote 0 down vote favorite If the URL is correct (file.dat exists), this works great (the file length matches). If it is wrong I will see a very small file length and I will not see the xhr.onerror. How can I detect that the URL was incorrect? var http://stackoverflow.com/questions/33308932/xmlhttprequest-detecting-404-not-found xhr = new XMLHttpRequest() xhr.responseType = "blob" xhr.onload = ()=> { var reader = new FileReader() reader.onload = evt => { var contents = new Buffer(evt.target.result, 'binary') console.log('file len',contents.length) } reader.readAsBinaryString(xhr.response) } xhr.addEventListener("error", () => { console.error('xhr.onerror',e) }) xhr.open("GET", "file.dat") xhr.send() https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest I do see a stacktrace in the console pointing to xhr.send() GET http://localhost:8080/file.dat 404 (Not Found) A try catch around both open and send does not catch any exceptions. Files are served by WebpackDevServer (I hope that should not matter though). javascript share|improve this question edited Oct 23 '15 at 18:23 asked Oct 23 '15 at 18:14 jcalfee314 1,3671537 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted You can check the status of the response object. // Not using arrow function because I don't want the lexical `this` xhr.onload = function() { if (this.status === 404) { // not found, add some error handling return; } var reader = new FileReader() reader.onload = evt => { var contents = new Buffer(evt.target.result, 'binary') console.log('file len',contents.length) } reader.readAsBinaryString(xhr.response) } Credit to https://developer.appcelerator.com/question/129410/xhr-request-cant-check-for-error-for-404-page-or-
and August 2005 versions. This version January 2006. Internet Explorer on Windows, Safari on Mac OS-X, Mozilla on all platforms, Konqueror in http://www.jibbering.com/2002/4/httprequest.html KDE, IceBrowser on Java, and Opera on all platforms including Symbian provide a method for client side javascript to make HTTP requests. From the humble begins as an oddly named object with few admirers, it's blossomed to be the core technology in something called AJAX [1]. The Object makes many things easier and neater than they other would be, and introduces some things 404 error that were otherwise impossible such as HEAD requests to see when a resource was last modified, or to see if it even exists. It makes your scripting options more flexible allowing for POST requests without having the page change, and opens up the possibility of using PUT, DELETE etc. These methods are increasingly used to provide richer Web Applications like G-Mail that use javascript xmlhttprequest 404 lower bandwidth and offer snappier user interaction. Why XML HTTP Request object? Whilst the object is called the XML HTTP Request object it is not limited to being used with XML, it can request or send any type of document, although dealing with binary streams can be problematical in javascript. Creating the object In Internet Explorer, you create the object using new ActiveXObject("Msxml2.XMLHTTP") or new ActiveXObject("Microsoft.XMLHTTP") depending on the version of MSXML installed. In Mozilla and Safari (and likely in future UA's that support it) you use new XMLHttpRequest() IceBrowser uses yet another method the window.createRequest() method. This means that you need to show different script to different browsers, as what works in one, will error in another. The script below does this, and if it's not supported, the variable is set to false to allow for appropriate error messages and recovery with degrading to more normal HTTP transaction methods when the object isn't available. This degradation is important, even in IE the objects can often be blocked by slightly raised security settings (popular due to the commonly exploited holes of course). Where possible degrade, some approaches