[英]Show Response of Ajax request
public ArrayList getConsignmentsID(final DB database, String searchValue) { System.out.println("inside getConsignmentsID function"); Consignment consignment = new Consignment(); DBConnection dbConnection = new DBConnection("mongodb://localhost", "transport"); dbConnection.open(); DBCollection conCollection = dbConnection.getDatabase().getCollection("consignment"); List dbCursor = null; if (searchValue != null) { BasicDBObject searchObj = new BasicDBObject("_id", new BasicDBObject("$regex", "^" + searchValue)); dbCursor = conCollection.find(searchObj, new BasicDBObject("_id", "true")).toArray(); dbConnection.close(); ArrayList consignmentList = new ArrayList(); for (DBObject x: dbCursor) { System.out.println(x); consignmentList.add(x.get("_id")); } return consignmentList; } else { return null; } //System.out.println(new > BasicDBObject("consignmentList",consignmentList)); }
By Ajax Call i am getting this response from the json string in javascript 通过Ajax调用我正在从javascript中的json字符串获取此响应
{ "consignmentList" : [ "" , "AAA" , "ABC" , "BHU" , "MAN" , "WER" , "ZXC"]} {“ consignmentList”:[“”,“ AAA”,“ ABC”,“ BHU”,“ MAN”,“ WER”,“ ZXC”]}
and i want to parse this array string in javascript and display its values in the unodered list. 我想在javascript中解析此数组字符串,并在无序列表中显示其值。 and the backend response from the database is. 数据库的后端响应是。
{ "_id" : ""} { "_id" : "AAA"} { "_id" : "ABC"} { "_id" : "BHU"} { "_id" : "MAN"} { "_id" : "WER"} { "_id" : "ZXC"} {“ _id”:“”} {“ _id”:“ AAA”} {“ _id”:“ ABC”} {“ _id”:“ BHU”} {“ _id”:“ MAN”} {“ _id”:“ WER“} {” _id“:” ZXC“}
function autocomplet() { var min_length = 0; // min caracters to display the autocomplete var consignmentID = $('#consignmentID').val(); var consignmentList = $('#consignmentList'); if (consignmentID.length >= min_length) { $.ajax({ url: '/jqueryreturn', type: 'POST', datatype: JSON, data: { consignmentID: consignmentID }, success: function(data) { consignmentList.show(); consignmentList.html(data); } }); } else { $('#consignmentList').hide(); } } // set_item : this function will be executed when we select an item function set_item(item) { // change input value $('#consignmentID').val(item); // hide proposition list $('#consignmentList').hide(); }
<div ><label style="margin:15px 0 0 0;" >Consignment:</label><input onkeyup="autocomplet()" id="consignmentID" type="text" class="inputlt" name="consignmentId" value="${(consign._id)!""}" id="c" style="font-size: 16px " onclick="clearInput(this)"> <ul id="consignmentList"></ul>
function autocomplet() {
var min_length = 0; // min caracters to display the autocomplete
var consignmentID = $('#consignmentID').val();
var consignmentList = $('#consignmentList');
if (consignmentID.length >= min_length) {
$.ajax({
url: '/jqueryreturn',
type: 'POST',
datatype: JSON,
data: {
consignmentID: consignmentID
},
success: function(data) {
consignmentList.show();
consignmentList.html(data);
}
});
} else {
$('#consignmentList').hide();
}
}
// set_item : this function will be executed when we select an item
function set_item(data) {
// change input value
$('#consignmentID').val(data);
// hide proposition list
$('#consignmentList').hide();
}
<div ><label style="margin:15px 0 0 0;" >Consignment:</label><input onkeyup="autocomplet()" id="consignmentID" type="text" class="inputlt" name="consignmentId" value="${(consign._id)!""}" id="c" style="font-size: 16px " onclick="clearInput(this)">
<ul id="consignmentList"></ul>
You can do it this way... 你可以这样
success: function(data) {
var res = "";
for(var i=0; i<data.consignmentList.length; i++)
{
res +="<li>"+data.consignmentList[i]+"</li>";
}
consignmentList.html(res).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.