【发布时间】:2016-03-22 20:00:32
【问题描述】:
谁能告诉我我的代码有什么问题?我认为这是一个语法错误,因为我对 Javascript/JSON 不是很熟悉。目前,这段代码什么都不做。这是学校项目;我的班级正在构建一个应用程序,该应用程序通过提供一个基于空闲时间/课程安排来预订活动的界面来连接用户。谢谢!
<!DOCTYPE html>
<html>
<head>
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#submit").click(function(){
var url = "http://3920project.azurewebsites.net/api/activity/";
//var email = "?email=" + getUrlVars();
//var title = "?title=" + "girls";
var title = "?title=" + getTitle();
//alert(url + title);
$.getJSON(url + title)
// data: {title: $("#title").val()
.done(function(res) {
//alert("done");
$.each(res, function(key, item) {
var colID = $('<td>', {
text: item.category
});
var colName = $('<td>', {
text: item.day + item.to + item.from
});
$('<tr>').append(colID, colName).appendTo($('#tblResult:last'));
}); //end of each()
}); //end of done()
}); //end of click()
}); //end of ready()
function getTitle() {
return document.getElementById('title').value;
}
</script>
<style type="text/css">
body {
font-family: arial;
}
th,
td {
margin: 0;
text-align: center;
border-collapse: collapse;
outline: 1px solid #e3e3e3;
}
td {
padding: 5px 10px;
}
th {
background: #666;
color: white;
padding: 5px 10px;
}
td:hover {
cursor: pointer;
background: #666;
color: white;
}
</style>
<title>Activity Search</title>
</head>
<body>
<div class="app">
Please enter activity:
<br></br>
<input type="text" id="title">
<input type="submit" id="submit" value="Search" />
<br></br>
</div>
<div>
<h2>Activity TimeTable</h2>
<table id="tblResult">
<tr>
<th>Activity</th>
<th>Category</th>
</tr>
</table>
</div>
</body>
</html>
【问题讨论】:
-
你用 chrome 开发工具检查 js 错误了吗?
-
检查您的
res。这是一个字符串。使用JSON.parse(res)创建对象 -
我的同学有类似的任务,她没有使用 JSON.parse(res) 并且她的工作正常。我认为我的问题与 .click 函数有关,因为它是少数与她不同的事情之一,因为她不需要用户输入。
-
您的文档中没有ID为
title的输入。 -
@Wainage 当你使用
.getJSON时,它会自动解析它。这就是.get和.getJSON之间的区别。
标签: javascript html json api