Display the result from an Ajax call

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)); }

Through an Ajax call, I am receiving this response from the JSON string in JavaScript

{ "consignmentList" : [ "" , "AAA" , "ABC" , "BHU" , "MAN" , "WER" , "ZXC"]}

I wish to parse this array string in JavaScript and display its values in an unordered list. Here is the backend response from the database.

{ "_id" : ""} { "_id" : "AAA"} { "_id" : "ABC"} { "_id" : "BHU"} { "_id" : "MAN"} { "_id" : "WER"} { "_id" : "ZXC"}

function autocomplet() {
    var min_length = 0; // minimum characters 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; // minimum characters 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>

Answer №1

Here's a method you can use...

function successfulResponse(data) {
   var results = "";
   for(var index=0; index<data.consignmentList.length; index++)
    {
       results +="<li>"+data.consignmentList[index]+"</li>";
    }
    consignmentList.html(results).show();

            }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering an error while trying to launch Chrome with Puppeteer

Currently, I have set up an elastic-beanstalk instance on AWS and am in the process of creating a pdf export feature on a dashboard using Puppeteer. Although I have successfully tested the application locally, I encountered an error when attempting to run ...

Determining the address type in an ajax call within jQuery Mobile: Absolute vs Relative

After testing the code, it seems to be functioning properly on iPads and iPhones (4,5) in Safari and Chrome. However, there is an issue with the ajax call when using Android devices and desktop browsers, as it consistently goes straight to the onError func ...

How to use jQuery to scroll to the top of a specific container div

The accordion menu I am currently using was created by Ian Flynn of RocketMill and can be found here: Although this accordion has served me well in the past, I have encountered a new challenge with a client who prefers to have more content. This causes is ...

Add a variety of parameters to the URL in order to make an AJAX request

I want to dynamically add values from checked checkboxes to a URL for use in an AJAX call. I only want these values to be added to the URL if the checkbox is checked. If a user checks and then unchecks, I do not want that value included in the URL. Below ...

The concept of localStorage is not recognized in the next.js framework

Currently working with next.js for frontend development. Facing an issue where I need to access user data from localStorage, but due to next.js being a server-side rendering framework, I am unable to retrieve data from localStorage. How can I solve this pr ...

Python code for performing a surgical replacement of a JSON field with another in a file

Currently, I am dealing with a large collection of JSON files structured like this: File00, timestamp T1: { "AAA": { "BBB": { "000": "value0" }, "CCC": { "111": "value1", "222": "value2", "333": "value3" }, " ...

Unique rewritten text: "Special case: This is not defined

I am in the process of creating my own custom exception in Node.js to handle errors specific to the environment the server is running in. However, when the error is thrown, it shows as undefined. (function () { 'use strict'; var states = req ...

What is the technique for moving a slider-like checkbox slider to one of its labels when they are clicked on?

Is there a way to make the switch slider move to the word the user clicked on (medium or large)? I'm not sure if I can track the movement of the switch slider with JavaScript or if it's possible to do with CSS. Right now, I have only created a si ...

What is the process for sending Raw Commands to a Receipt Printer using Node.JS?

My Current Project I am currently working on integrating a receipt printer that supports ESC/P raw printing into an app for remote printing of receipts. The Approach I Am Taking To achieve this, I am utilizing the PrintNodes API to send data from my app ...

Issue with zeroLineColor and zeroLineWidth not functioning properly for the x-axis within Chartjs

Looking to customize the width and color of the x-axis in Chartjs? While it's straightforward for the y-axis using zeroLineColor and zeroLineWidth, achieving the same effect for the x-axis seems trickier. If you try adding: ticks: { beginAtZero: tr ...

The data variable is typically implemented as __ob__ when it is being returned

Currently, I am working on creating a line chart using vue-chart.js. Here is the Vue component I am utilizing: Vue.component("line-plot", { extends: VueChartJs.Line, props: ["label", "data", "options"], mounted(){ ...

Steps for creating interconnected datepickers in jQuery with month and year selection:To create a dependent datepicker with month and year selection using

The JSFiddle I currently have is not functioning as expected. When the user directly selects the end-date, it does not restrict the start-date in this particular fiddle. It should change once the user directly changes the end-date. The functionality works ...

Function in Typescript that accepts an extended interface as a parameter

I am facing an issue with my interface named "Example" which has a function type called "exampleFunction". The problem arises when this function takes a super class as an input parameter because TypeScript is reporting an error. It states that I cannot use ...

How to prevent multiple instances of setTimeout executing

I am facing a particular issue with my music application development using JavaScript and other technologies. I have analyzed the module architecture in Engine and UI, and the problem seems to be within the Engine Modules. Specifically, I have a main Engin ...

The SessionFactory encountered a null pointer exception while trying to open a session

After spending several days working with Hibernate and MySQL, I encountered a NullPointerException error in my setUpSession.java file while running the program as a JUnit test. The error is related to the sessionFactory variable. My main objective at the ...

Developing versatile form elements with Formik and the Yup library for React and Vite

I'm currently working on a React and Vite project where I'm trying to implement reusable form components using Formik and Yup, but I haven't been able to achieve it yet. I've created a component called <AppInputField/>, which is e ...

Attempting to start the Android AVM/SDK GUI through the tools/android executable in the Android SDK is proving to be a challenge on Ubuntu 10.04 i686

Greetings! After setting up the Android SDK and Eclipse 3.5 on my i686 Thinkpad T60 with Ubuntu 10.04 (Lucid), I encountered an issue. Although Eclipse add-on installation was successful and I added the android-sdk tools directory to my user PATH, I ran in ...

What could be causing the JavaScript alert window to not display new lines properly?

I have encountered several issues with the alert window and new lines. One major problem is that the \n character is interpreted as a new line in PHP instead of being sent to JavaScript. In my situation, when the string is displayed in a new window, ...

Extract data from a JSON field within a Snowflake table and insert the extracted information as multiple rows into a new Snowflake

I am facing a challenge with the 'reactions' field/column in a Snowflake table named 'tbl'. This table consists of multiple columns and records, with the 'reactions' field being a json array. The json data within this field in ...

Issues with navigating sliders

I've encountered a problem with my slider arrows while following a YouTube tutorial. They are supposed to appear next to the picture and testimonial, but instead, they are showing up at the top. This issue wasn't present initially, but after enco ...