2
votes

I am following Lynda tutorials on Javascript and Ajax and hungup with this problem on topic "Using Synchronous XHR request".

Basically the html file is:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>JavaScript AJAX</title>
</head>
<body>
<script src = "script.js"></script>
</body>
</html>

and the javascript file is:

var request = new XMLHttpRequest();
request.open('GET', 'data.txt', false);
request.send();
console.log(request);

the data.txt file has "Hello World" on it.

the path to the project files are

C:/wamp/www/ajax/index.html
C:/wamp/www/ajax/script.js

When i open the localhost on wampserver and do inspect element i get this above error saying "Failed to load resource: the server responded with a status 404(not found)"

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/. http://localhost/ajax/script.js Failed to load resource: the server responded with a status of 404 (Not Found)

XMLHttpRequest

onabort: null
onerror: null
onload: null
onloadend: null
onloadstart: null
onprogress: null
onreadystatechange: null
ontimeout: null
readyState: 4
response: "<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">↵<html><head>↵<title>404 Not Found</title>↵</head><body>↵<h1>Not Found</h1>↵<p>The requested URL /ajax/data.txt was not found on this server.</p>↵<hr>↵<address>Apache/2.4.9 (Win32) PHP/5.5.12 Server at localhost Port 80</address>↵</body></html>↵"
responseText: "<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">↵<html><head>↵<title>404 Not Found</title>↵</head><body>↵<h1>Not Found</h1>↵<p>The requested URL /ajax/data.txt was not found on this server.</p>↵<hr>↵<address>Apache/2.4.9 (Win32) PHP/5.5.12 Server at localhost Port 80</address>↵</body></html>↵"
responseType: ""
responseURL: "http://localhost/ajax/data.txt"
responseXML: null
status: 404
statusText: "Not Found"
timeout: 0
upload: XMLHttpRequestUpload
withCredentials: false
__proto__: XMLHttpRequest
1

1 Answers

3
votes

hungup with this problem on topic "Using Synchronous XHR request".

So don't do that.

request.open('GET', 'data.txt', false);

The false means "Don't make an asynchronous request". Take it out.

request.open('GET', 'data.txt');

You'll then need to use an event listener instead of expecting the browser to wait until the response comes back before continuing to execute the JavaScript.

request.addEventListener("load", function (event) {
    console.log(this);
});

That, of course, has nothing to do with:

Failed to load resource: the server responded with a status of 404 (Not Found)

Which just means that http://localhost says /ajax/data.txt doesn't exist.