Ajax将数据发送到控制器。方法在单击按钮时使用该数据。我的方法有model.addatribute.Alert
个null
个值。为什么呢?
我的控制器
@RequestMapping(value = "index", method = RequestMethod.POST)
@ResponseBody
public List<String> field(@RequestParam("tName") String name,
@RequestParam("dName") String dname, Model model) {
String tn = name;
String dn = dname;
List<String> coname = new ArrayList<>();
try {
Connection mycon2 = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "1996");
DatabaseMetaData metaData2 = mycon2.getMetaData();
ResultSet res4 = metaData2.getColumns(dn, null, tn, "%");
coname = new ArrayList<>();
while (res4.next()) {
String column_name = res4.getString("COLUMN_NAME");
coname.add(column_name);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return coname;
}
索引HTML
<div id="RightPanel" style="width: 200; height: 100%; float: right;">
<table class="table table-striped">
<input type='text' id='tablehead'/>
<tr>
<th>Column name</th>
</tr>
<tr th:each="name : ${cname}">
<td> th:text="${name}"</td>
</tr>
</table>
</div>
索引html中的JavaScript
<script th:inline="javascript">
function onclk(a, b) {
var search = {
"tName": a,
"dName": b
};
$.ajax({
type: "POST",
url: "http://localhost:8092/index",
data: search,
dataType: "json",
success: function (data) {
var modelAttributeValue = [[${coname}]];
alert(modelAttributeValue);
},
error: function () {
alert('got error');
}
});
}
</script>
答案 0 :(得分:0)
试试这个:
<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
function onclk( a,b) {
var search = {
"tName" : a,
"dName" :b
}
$.ajax({
type: "POST",
url: "http://localhost:8092/index",
data: search,
dataType: "json",
success: function (data) {
var modelAttributeValue = [[${coname}]];
alert(modelAttributeValue); },
error: function(){
alert('got error');
}
});
/*]]>*/
</script>