Load data into semantic-ui search component using ajax requests

Having trouble populating the semantic-ui search component with JSON data retrieved from my server. The documentation is not helpful and I haven't been able to find a solution online. Here's what I have so far, appreciate any help.

This snippet is from index.jsp

    <div class="ui search">
    <div class="ui icon input">
        <input id="searchFollowee" class="prompt" type="text" placeholder="Search followed people..">
            <i class="search icon"></i>
    </div>
    <div class="results"></div>
    </div>

Below is the JavaScript code snippet:

    $("#searchFollowee").on('keyup', function(e) {
     $.post("jsp/doFetchFollowee.jsp",{"username":"Manfredi","followee":$(this).val()}, function( data )
              {
                    var content=[]; 
                    $.each(data, function(index,element)
                    {
                        content.push(element.username);
                    });
                  //fill search form
                    $('.ui.search').search({
                      source : content,
                      searchFields   : [
                        'username'
                      ],
                      searchFullText: false
                    });
              });
     });

And here is the response from the server:

    {  
    "success":"true",
    "results":[  
    {  
        "firstname":"NomeTest1",
        "user_id":2,
        "surname":"CognomeTest1",
        "profile_picture":"semantic/themes/default/assets/images/avatar/large/profile.png",
        "username":"Test1"
    },
    {  
        "firstname":"NomeTest3",
        "user_id":4,
        "surname":"CognomeTest3",
     "profile_picture":"semantic/themes/default/assets/images/avatar/large/profile.png",
        "username":"Test3"
    }
    ]
}

Answer №1

The line causing the issue is here:

$.each( data, function(index,element)

To resolve based on your server's reply, update it to this:

$.each( data.results, function(index,element)

var content=[];
var data = {
    "success":"true",
    "results":[
        {
            "firstname":"NomeTest1",
            "user_id":2,
            "surname":"CognomeTest1",
            "profile_picture":"semantic/themes/default/assets/images/avatar/large/profile.png",
            "username":"Test1"
        },
        {
            "firstname":"NomeTest3",
            "user_id":4,
            "surname":"CognomeTest3",
            "profile_picture":"semantic/themes/default/assets/images/avatar/large/profile.png",
            "username":"Test3"
        }
    ]
};
$.each( data.results, function(index,element)
{
    content.push({title: element.username});
});

//populate search form
$('.ui.search').search({
    source : content,
    searchFields   : [
        'title'
    ],
    searchFullText: false
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/components/search.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/components/search.js"></script>


<div class="ui search">
    <div class="ui icon input">
        <input id="searchFollowee" class="prompt" type="text" placeholder="Search followed people..">
        <i class="search icon"></i>
    </div>
    <div class="results"></div>
</div>

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

Addressing simple JavaScript "async" AJAX requests that are sequenced and reliant on each other

I'm currently developing an application that includes a dashboard on its main page. In order to address any potential loading issues, the content of the dashboard is being calculated asynchronously using Ajax. This means that users do not have to wait ...

Facing an issue with an Ajax script retrieving the PHP $_GET variable

Struggling to fetch PHP $_GET variable using an Ajax script? The aim is to update a MySql table with the same script. Below is my PHP/HTML script for fetching and displaying data: session_start(); require_once ('dbconnect.php'); $query = mysql_ ...

AJAX is not displaying the expected output

Seeking help on calling a PHP method from JavaScript to query a database and integrate the results into JS functionality. The current 'console.log(output)' in my Ajax returns: "array (size=1) 'action' => string 'getResults&apos ...

Live updating Google doughnut chart generated from real-time SQL query results

I am looking to create an interactive Google doughnut chart that displays data retrieved from a SQL query. Despite my efforts to find sample code, I have been unsuccessful in getting the chart to render on the page. Below is the script: <script type= ...

The JSON data updates with each new page load

My AJAX call retrieves data from a JSON file, where I calculate the length of the object and extract just the length. Initially, everything works smoothly. However, upon refreshing the page, the data is not displayed in the same order. // The URL is obtai ...

Using Jquery to verify an ajax server connection

Currently, I'm in the process of developing a web application using JQuery. One challenge I have encountered is ensuring that the code has access to the server before proceeding with any further actions. Below is the function I am currently using: fun ...

The dropdownlists mysteriously vanish forever after a modal popup, likely due to a timer issue

We are encountering some unexpected behavior with dropdown lists on a relatively complex webpage when using IE6. The layout of the page consists of 2 update panels, each containing a GridView that displays data in a master-details format. Additionally, eac ...

Ajax transmitting data with concealed characters

I'm trying to send data from one PHP site to another. On the first site, everything looks good. The string appears as --show="author,book,text/n However, when I check the string after receiving it on the second site, it shows --show="author,b ...

Utilize jQuery AJAX to showcase JSON data by handling the response of a POST request sent to a JSP

Displaying JSON Data Using JavaScript Code $(document).ready(function(){ $("button").click(function(){ $.ajax({ url:"jsonCreation.jsp", type:'post', dataType: 'json', ...

"Encountering an issue with AJAX file upload displaying an error message for

Before I showcase my code, allow me to explain my objective. My goal is to create a page that updates a user's details in the database using AJAX. Initially, I successfully achieved this task. Subsequently, I wanted to enhance the functionality by inc ...

Manage Blob data using Ajax request in spring MVC

My current project involves working with Blob data in spring MVC using jquery Ajax calls. Specifically, I am developing a banking application where I need to send an ajax request to retrieve all client details. However, the issue lies in dealing with the ...

Customizing the OPTIONS method in SpringBoot

When I make an ajax call to the 'OPTIONS' method in my rest-controller (implemented using SPRINGBOOT), I am receiving two responses - one by default and the other from the implemented method. I'm trying to figure out where the second respons ...

JSF Ajax call: Invoking a JavaScript function at the completion

Working on a project with JSF 2.0, here is the form being used: <h:form id="fff" onsubmit="reloadMap();"> <h:selectOneMenu value="#{rentCarPoolBean.state}"> <f:selectItems value="#{rentCarPoolBean.stateList}" id="stateList" /> ...

Is there a way to prevent an ajax call from refreshing the page?

I've implemented image upload functionality in my project. However, when I click the upload button and select a file, the images are displayed via an AJAX call with HTML code. Unfortunately, afterwards, the page refreshes and all form data is lost. E ...

Utilizing an undefined constant in angular programming may lead to unexpected errors

Seeking assistance in generating random first and last names based on gender for a form using Angular. I am relatively new to Angular and keep encountering this error whenever the Use of undefined constant firstName - assumed 'firstName' Here ...

Consecutive AJAX requests triggered by previous response

While there are numerous resources available regarding making synchronous AJAX calls using deferred promises, my specific issue presents a unique challenge. The process I am attempting to facilitate is as follows: Initiate an AJAX call. If the response i ...

What results can be expected from a piped file stream?

Perhaps the wording of the question may not be perfect, but here is some additional context. With GridFSBucket, I am able to store a file in MongoDB and retrieve a download stream for that file. Here's my query: If I wanted to send that file back as a ...

When does an xmlHttpRequest object parse serialized XML into a DOM during its lifecycle?

When a JavaScript code with xmlHttpRequest.responseXML() runs, it creates a DOM Document object from the XML-structured HTTP response body. Have you ever wondered at what moment the XML string is turned into the DOM Document by an xmlHttpRequest object? ...

When an Ajax call is made, my .html page transforms into a .php page using jQuery

The Issue While using my PHP function to send emails, everything goes smoothly. However, I'm facing an issue where I don't want the page to refresh, so I've implemented AJAX in the code below: reservation.html <form class="form-horizon ...

sending a JSON object from the controller to a JSP page

I need to transfer all the data from my database in the form of a JSON array to a JSP page so that it can be retrieved using AJAX. EmployeeController public class EmployeeController { @Autowired private EmployeeService employeeService; @RequestMapping( ...