我正在尝试对servlet进行ajax调用,并在自动建议中显示数据。一切似乎都正常,但JQuery中的数据对象错误“未定义数据”。请找到下面的代码。请你帮助我好吗 ?谢谢!
$(document).ready(function () {
$( "#companyInfo" ).autocomplete({
source: function( request, response ) {
console.log("Testing:");
$.ajax( {
url: "/pfWaldo/RetrieveCompanyData",
dataType: "jason",
data: {
term: request.term
},
success: function( data ) {
console.log("data: ", data);
response( response );
}
} );
},
minLength: 2,
select: function( event, ui ) {
console.log( "Selected: " + ui.item.value + " aka " + ui.item.id );
}
} );
} );
Servlet代码:
response.setContentType(“ application / json”);
response.setHeader("cache-control", "no-cache");
ObjectMapper mapper = new ObjectMapper();
String path = null;
try {
ArrayList<CompanyInfoBO> companyInfoBOList = new ArrayList<CompanyInfoBO>();
CompanyInfoBO companyInfoBO1 = new CompanyInfoBO("1","11111");
CompanyInfoBO companyInfoBO2 = new CompanyInfoBO("2","22222");
CompanyInfoBO companyInfoBO3 = new CompanyInfoBO("3","33333");
CompanyInfoBO companyInfoBO4 = new CompanyInfoBO("4","44444");
CompanyInfoBO companyInfoBO5 = new CompanyInfoBO("5","55555");
CompanyInfoBO companyInfoBO6 = new CompanyInfoBO("6","66666");
CompanyInfoBO companyInfoBO7 = new CompanyInfoBO("7","77777");
companyInfoBOList.add(companyInfoBO1);
companyInfoBOList.add(companyInfoBO2);
companyInfoBOList.add(companyInfoBO3);
companyInfoBOList.add(companyInfoBO4);
companyInfoBOList.add(companyInfoBO5);
companyInfoBOList.add(companyInfoBO6);
companyInfoBOList.add(companyInfoBO7);
path = "{\"success\": true, \"}";
String dataJson = mapper.writeValueAsString(companyInfoBOList);
response.getOutputStream().print(dataJson);
//Finally send the jason response
OutputStream out = response.getOutputStream();
mapper.writeValue(out, dataJson);
在调试模式下显示Jason对象的Servlet代码。