我正在关注关于Javascript和Ajax的Lynda教程,并在“使用同步XHR请求”主题上解决了这个问题 .
基本上html文件是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JavaScript AJAX</title>
</head>
<body>
<script src = "script.js"></script>
</body>
</html>
和javascript文件是:
var request = new XMLHttpRequest();
request.open('GET', 'data.txt', false);
request.send();
console.log(request);
data.txt文件上有“Hello World” .
项目文件的路径是
C:/wamp/www/ajax/index.html
C:/wamp/www/ajax/script.js
当我打开wampserver上的localhost并执行检查元素时,我得到上面的错误,说“无法加载资源:服务器响应状态为404(未找到)”
主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用 . 如需更多帮助,请查看http://xhr.spec.whatwg.org/ . http://localhost/ajax/script.js无法加载资源:服务器响应状态为404(未找到)
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 回答
所以不要这样做 .
false
表示"Don't make an asynchronous request" . 拿出来 .然后,您需要使用事件侦听器,而不是期望浏览器等待响应返回,然后再继续执行JavaScript .
当然,这与以下内容无关:
这只是意味着
http://localhost
说/ajax/data.txt
不存在 .