JSON objects not loading properly in Bootstrap table

I am facing an issue where my ajax script successfully sends JSON objects to the browser, but the table fails to load the JSON object. Here is my Ajax script:

 $.ajax({
            type : "POST",
            url : "getLabels.jsp",
            data : "mailingID=" + selectedValue, // posCodeSelected
            success : function(data) {

                response = $.parseJSON(data);// this statement sends data successfully to the browser

            },
            error : function(response) {
                var responseTextObject = jQuery.parseJSON(response.responseText);
            }
        });

Below is the Bootstrap table embedded into my JSP page:

<table data-height="299" data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar1">
    <thead>
    <tr>
        <th data-field="rowNumber" >ID</th>
        <th data-field="firstname" >first name</th>
        <th data-field="lastname" >last name</th>
        <th data-field="organization" >organization</th>
        <th data-field="city" >city</th>
        <th data-field="state" >state</th>

    </tr>
    </thead>
</table>

Also, just to update everyone, this is the JSON response in the browser:

{"rowNumber":1,"mailingID":3,"firstname":"Brian","lastname":"Fairhurst","organization":"Florida State University","city":"Tallahassee","state":"FL"}

Answer №1

To incorporate the JSON object data into your table, follow these steps:

// Ensure that your table in the HTML has the id="tbl"

var table = document.getElementById("tbl");

var row = table.insertRow(table.rows.length); // Add a new row

// Insert cells for the new row

cells = [];

for(var i = 0; i < 6; i++){
    cells[i] = row.insertCell(i);
}

// Populate the cells with data from the JSON object

cells[0].innerHTML = response.rowNumber;
cells[1].innerHTML = response.firstname;
cells[2].innerHTML = response.lastname;
cells[3].innerHTML = response.organization;
cells[4].innerHTML = response.city;
cells[5].innerHTML = response.state;

Answer №2

The issue arises from the fact that the data in the response is not populating the HTML table as intended. To resolve this, include the data-url attribute within your HTML table.

Instead of:

<table data-height="299" data-show-refresh="true" ...>
    <thead>
    ...

You should utilize:

<table data-height="299" data-show-refresh="true" data-url="response"...>
    <thead>
    ...

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

execute numerous queries simultaneously

I have a task of creating a bridge (script) that connects two databases, Mongo and Oracle. First, I execute three find queries on my MONGO database from three different collections: Collection 1 = [{ name: 'Toto', from: 'Momo', ...

When triggered by a click, the function gradually fades in and out. It superimposes one image on top of another, but unfortunately, the sizes

Due to different screen sizes, the image does not appear on top of another image exactly. It seems like a new function is needed. One that does not overlap with another image (by clicking the function for a few seconds), but rather replaces it for those fe ...

Node.js tutorial: Fetching all pages of playlists from Spotify API

Currently, I am attempting to retrieve all of a user's playlists from the spotify API and display them in a selection list. The challenge lies in only being able to access 20 playlists at a time, which prompted me to create a while loop utilizing the ...

What is the best way to apply CSS to a single div without affecting the rest of the code?

After importing the following stylesheet: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> I noticed that it was affecting other elements on my webpage, like buttons. Is there a way to instruct m ...

Is there a way to utilize jquery to click a submit button within a concealed form?

In the current setup, there are 25 products displayed on a single page. Each product contains a form that needs to be dragged to the mini cart located on the right side. The challenge lies in identifying the form ID of the product that is being dragged. T ...

Guide to inspecting file contents with Node.js

I am working on viewing the content of a file that is posted from the client using the fs module. However, with the code below, the contents are coming up as undefined. Can anyone help me identify what is missing in the code? To ensure I am receiving the ...

What is the best way to retrieve the name/value pairs from a JSON object

function (data) { //add values based on activity type //data = JSON.parse(data); //alert(abc.Phone1); alert(data.myName) alert(data.toString()); if (activityType == "Phone") { } return; }, By observing the callback funct ...

What is the method for inserting data into an array of objects in JavaScript?

I have a question regarding how to push/replace data into an object of an array of objects. Please excuse any mistakes in my grammar. Here is my dummy data: const dummyData = { id: 1, daerah: "Bandung", date:"1668790800000& ...

Creating an array based on specific conditions being satisfied (Using Javascript)

I am struggling with a coding problem involving adding users to groups. The goal is to add each user to a group array, with a maximum of 3 users per group. If a group reaches 3 users, it should be moved to another array that collects all the groups. I then ...

Toggling the form's value to true upon displaying the popup

I have developed an HTML page that handles the creation of new users on my website. Once a user is successfully created, I want to display a pop-up message confirming their creation. Although everything works fine, I had to add the attribute "onsubmit= re ...

An elaborate warning mechanism in Redux-observable that does not trigger an action at the conclusion of an epic

I'm currently working on implementing a sophisticated alert system using redux and redux-observable. The requirements are: An action should request an alert: REQUEST_ALERT An action should create an alert and add an ID: SET_ALERT (handled in the ep ...

The database does not get updated by an Ajax request without a page refresh

I'm currently in the process of developing a dashboard system and I've created a ToDo list for it. The main functionality of the system is to allow users to add, delete, and mark tasks as finished or unfinished. I am using codeIgniter for this p ...

Utilizing Laravel's Eloquent ORM to filter columns containing JSON data

Currently, I am developing a data filter that involves filtering a column containing JSON formatted data. The table has a structure resembling the following: ____________________________________ | id | name | tags | |____|_________|_______ ...

Struggling to generate fresh vue components

Having trouble registering new components in my Vue app. I have successfully registered some components, but when I try to register a new one, I encounter the error: Unknown custom element: <store> - did you register the component correctly? For re ...

Changing the user object stored in the database within the next authentication process following registration

In my next.js application, I have implemented Next Auth for authentication and used a database strategy. Once a user logs in, is there a way to update their data? ...

Using jQuery to select elements with specific innerHTML values

$('.select option:selected[innerHTML="5"]') In this case, I am attempting to choose an option that is currently selected and has innerHTML that perfectly matches a specific string (for example: "5") For reference, here is a fiddle link: http:// ...

Select the text inside the current cell of a jqGrid table

The issue at hand is that when using jqGrid with cellEdit:true, users are unable to select text in a cell. Once the mouse button is released, the selection resets. This limitation prevents users from copying the selected text within cells. Has anyone enco ...

How to retrieve the value of a selected radio button in an AngularJS radio button group that uses ng-repeat

In the following code snippet, I am trying to retrieve the value when any of the radio buttons is selected: <label ng-repeat="SurveyType in SurveyTypes"> <input type="radio" name="SurveyTypeName" ng-model="surveyData.SurveyTypeN ...

Update information without the need for an xhr request in the back-end using jQuery

To keep notification updated with any changes in the database without causing a slowdown to the website, I implemented the following solution: HTML Code: <div id="myid">{{ $db_data }}</div> Back-end Request (utilizing Laravel): $db_data = A ...

Is there a way to showcase a modal following a timed delay?

I want my modal to appear 2 seconds after the page loads. I've tried setting the state in componentDidUpdate, but I keep getting active: undefined. The active props control the visibility of the modal on the page. When I toggle it to true using the Re ...