我堅持使用jQuery自動完成(I am trying this example)自動完成功能,使其運行
我沒有得到的是什麼問題:沒有消息在瀏覽器控制檯顯示。
URL被調用,服務器端的輸出被正確打印。
自動完成未在JSP頁面中顯示。
JSP頁面
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<style>
.ui-autocomplete-loading {
background: white url('images/loader.gif') right center no-repeat;
}
</style>
</head>
<body>
<div class="box">
<span class="label">Names</span>
<span class="ib"> <input type="text" name="name" id="name"/></span>
</div>
<script>
$(function() {
function log(message) {
$("<div>").text(message).prependTo("#log");
$("#log").scrollTop(0);
}
$("#name").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://localhost:8080/cxn/test",
dataType: "jsonp",
data: request,
success: function(data) {
response($.map(data.names, function(item) {
console.log(item);
return {
label: item.names,
value: item.names
}
}));
},
error: function(data) {
alert(data.names);
console.log(data);
alert('error');
}
});
},
minLength: 2,
select: function(event, ui) {
log(ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function() {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
});
});
</script>
</body>
在行動中配置
<action name="test" class="iland.supplier.SupplierAction" method="test">
<result type="json"></result>
</action>
在actionClass
public class SupplierAction extends ActionSupport {
private ArrayList Names;
//getter and setter of names
public String test() {
ArrayList<String> sss = new ArrayList<String>();
sss.add("Manish");
sss.add("Manoj");
sss.add("Mohan");
sss.add("Madhuri");
sss.add("Mayank");
sss.add("Mitesh");
sss.add("Mitali");
setNames(sss);
for (String s : sss) {
System.out.println(" " + s);
}
return SUCCESS;
}
}
這是我爲我努力以上自動完成JSON數據。我要爲名稱
這裏
error: function(data) {
alert(data.names);
console.log(data);
alert('error');
}
誤差得到所謂 alert(data.names) is undefined
和控制檯正在打印如下
你好我想上面的代碼,但不工作 – xrcwrn
我試過'錯誤:函數(數據){alert('error');}'。正在執行。如何糾正它 – xrcwrn
檢查控制檯以獲取有關錯誤的詳細信息。 – JoeFletch