【问题标题】:Failed to load resource: the server responded with a status 404(not found)加载资源失败:服务器响应状态为 404(未找到)
【发布时间】:2015-12-01 17:06:28
【问题描述】:

我在javascript和ajax上遵循lynda教程,并在主题“使用同步XHR请求”主题上挂起了Hungup。

基本上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(未找到)”

在主线程上的同步XMLHTTTPREQUEST由于其对最终用户的体验而受损的影响。如需更多帮助,请查看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

【问题讨论】:

    标签: javascript jquery html ajax wampserver


    【解决方案1】:

    在“使用同步 XHR 请求”主题上遇到这个问题。

    所以不要那样做。

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

    false 的意思是“不要发出异步请求”。把它拿出来。

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

    然后您需要使用事件侦听器,而不是期望浏览器等到响应返回后再继续执行 JavaScript。

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

    当然,这与以下无关:

    加载资源失败:服务器响应状态为 404(未找到)

    这意味着http://localhost 说/ajax/data.txt 不存在。

    【讨论】:

    • 谢谢!!!它工作得很好,正如你所说的那样,它与以下内容无关:“加载资源失败:服务器响应状态为 404(未找到)”。唯一的问题是我以“data.txt”的名称保存了我的文本文档,但实际上它必须只命名为“data”,因为它可以保存为作为文本文档的 data.txt。
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多