Ajax对JSON文件的请求不工作
Ajax request to JSON file not working
我有一个HTML表,我试图用存储在外部JSON文件中的数据填充。我试图使用纯JavaScript(没有JS库)对它进行AJAX请求,但当我点击"测试"按钮时,什么也没有发生;数据将无法填充。这是我的JSON文件:
{ "row":[
{
"ID" : "2",
"FirstName" : "John",
"LastName" : "Test",
"DOB": "03-12-1959",
"Gender":"M"
},
{
"ID" : "3",
"FirstName" : "Helen",
"LastName" : "Test",
"DOB": "03-12-1959",
"Gender":"M"
}
]
}
我的一些HTML代码:
<button onclick="loadJSON()">Test</button>
<table class="test-table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>DOB</th>
<th>Gender</th>
</tr>
</thead>
<tbody id="data">
<tr>
<td><label for="row1"></label>123</td>
<td>John</td>
<td>Doe</td>
<td>02-15-1982</td>
<td>M</td>
</tr>
</tbody>
</table>
和我的JavaScript代码:
function loadJSON(){
var data_file = "test.json";
var http_request = new XMLHttpRequest();
try{
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request.onreadystatechange = function(){
if (http_request.readyState == 4 ){
var jsonObj = JSON.parse(http_request.responseText);
var tr, td;
var tbody = document.getElementById("data");
// loop through data source
for (var i=0; i<jsonObj.row.length; i++) {
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute("align", "center");
td.innerHTML = jsonObj.row[i].ID;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].FirstName;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].LastName;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].DOB;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].Gender;
}
}
http_request.open("GET", data_file, true);
http_request.send();
}
我在哪里出错了,我能做些什么来解决这个问题?
在函数http_request.open()
之前漏了一个右括号}
…
用这个试试,会成功的。
HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button onclick="loadJSON()">Test</button>
<table class="test-table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>DOB</th>
<th>Gender</th>
</tr>
</thead>
<tbody id="data">
<tr>
<td>
<label for="row1"></label>123</td>
<td>John</td>
<td>Doe</td>
<td>02-15-1982</td>
<td>M</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function loadJSON() {
var data_file = "test.json";
var http_request = new XMLHttpRequest();
try {
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
var jsonObj = JSON.parse(http_request.responseText);
var tr, td;
var tbody = document.getElementById("data");
// loop through data source
for (var i = 0; i < jsonObj.row.length; i++) {
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute("align", "center");
td.innerHTML = jsonObj.row[i].ID;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].FirstName;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].LastName;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].DOB;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsonObj.row[i].Gender;
}
}
}; // <----- This is the one you left off
http_request.open("GET", data_file, true);
http_request.send();
}
</script>
</body>
</html>
JSON文件{
"row": [
{
"ID": "2",
"FirstName": "John",
"LastName": "Test",
"DOB": "03-12-1959",
"Gender": "M"
},
{
"ID": "3",
"FirstName": "Helen",
"LastName": "Test",
"DOB": "03-12-1959",
"Gender": "M"
}
]
}
相关文章:
- jQuery Ajax GET请求工作不正常
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- Javascript:JSON请求仅在第一次工作
- Jquery ajax方法请求体为null,但poster工作正常
- AWS API网关返回400错误请求,但Postman工作正常
- 只需添加一个'允许跨来源请求'到我的节点应用程序工作
- web工作程序中不同的服务器请求行为
- 如何让应用程序帮助程序方法在发送的请求为 JS 格式时工作
- Web 工作线程中的同步 XHR 请求是否仍会锁定浏览器
- 使用jQuery同步Ajax请求;我不能在Adobe Air上工作
- 跨源请求被POST阻止,但浏览器上的req可以正常工作,但设备上不工作
- API请求在使用phonegap调试时不工作,但在模拟器中工作,为什么
- 在处理请求工作时显示“正在处理”
- MVC 模型绑定无法通过 AJAX 请求工作
- 我不能让我的JSON请求工作
- 为什么post请求工作在本地而不是远程?搜索到的Url不同
- Ajax请求工作,但没有输出
- 如何循环大量的URL请求工作
- Ajax请求工作,但没有CSS和JavaScript
- 可以't设法使CORS请求工作