提问人:Heather Black 提问时间:10/31/2023 更新时间:10/31/2023 访问量:48
加载资源失败?
Failed to load resources?
问:
我一直被抛出这个错误 Failed to load resource: data.json:1 net::ERR_FAILED,但不知道为什么。这些文件是 'data.json'、'script.js'、'icd1ocm-codes-2024.html' 和 'stylesheet.css'。我在html或js中是否遗漏了某些内容?我尝试使用 fetch/getData(),但没有用。我已将文件放在共享的公用文件夹中。没有服务器,只有本地计算机文件可以使用。这用于通过 html 创建前端数据库
**JSON**
[
{
"code": "A00",
"description": "Cholera"
},
{
"code": "A01",
"description": "Typhoid and paratyphoid fevers"
},
// more code-description pairs...
]
// Retrieve the JSON data from the public folder
var xhr = new XMLHttpRequest();
xhr.open('GET', 'data.json', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
// Event listener for the search button
document.getElementById('searchButton').addEventListener('click', function() {
var searchTerm = document.getElementById('searchInput').value.toLowerCase();
var filteredResults = data.filter(function(item) {
return item.code.includes(searchTerm) || item.description.toLowerCase().includes(searchTerm);
});
displayResults(filteredResults);
});
}
}
};
xhr.send();
// Function to display the search results
function displayResults(results) {
var resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '';
if (results.length === 0) {
resultsDiv.textContent = 'No results found.';
} else {
results.forEach(function(result) {
var resultItem = document.createElement('div');
var codeSpan = document.createElement('span');
codeSpan.textContent = result.code + ': ';
var descriptionSpan = document.createElement('span');
descriptionSpan.textContent = result.description;
resultItem.appendChild(codeSpan);
resultItem.appendChild(descriptionSpan);
resultsDiv.appendChild(resultItem);
});
}
}
body{
background-color: lightblue;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.content-wrapper{
padding: 10px 30px;
}
p{
text-align: justify;
}
h1{
text-align: center;
}
.highlight{
font-weight: 700;
color: forestgreen;
}
h1, h2{
font-weight: 400;
}
ul li{
list-style-type: square;
margin-bottom: 10px;
margin-left: 50px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>ICD-10 Code/Description Search</title>
<meta type="text/html" charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="stylesheet.css" type="text/css"/>
</head>
<body>
<h1>ICD-10 Code/Description Search</h1>
<input type="text" id="searchInput" placeholder="Enter code or description">
<button id="searchButton">Search</button>
<div id="results"></div>
<script src="script.js" type="text/javascript"></script>
</body>
</html>
答:
0赞
CherryDT
10/31/2023
#1
这告诉您不能使用协议中的 XHR。(任何此类请求都将归类为 CORS,因为 URL 具有 null 来源。创建一个本地 Web 服务器并使用 ,然后它就可以工作了。file://
file://
http://
评论