Use Entity Objects instead of Strings in your Ajax requests for improved data handling

As I work on developing a web portal with Freemarker(FTL) as the front end technology, my project revolves around a single FTL page that utilizes a wizard to manage various user requests. The back end is structured using the Spring MVC pattern, where controllers process these user requests. AJAX calls are used to navigate the wizard and display data on the screen.

Whenever a button is clicked, a JavaScript function is triggered which then makes an AJAX call to invoke a controller method. This method executes specific business logic and returns the desired data.

Currently, my goal is to showcase the data retrieved through the AJAX call within a particular section of the FTL page. To achieve this, I'm utilizing @ResponseBody to deliver JSON responses to the AJAX call.

In my current implementation, I am loading the data into a specific division of the screen via AJAX. However, I find myself needing to include HTML tags in the JavaScript section for content loading purposes. Ideally, I would prefer not to mix HTML tags within the Ajax call section. My intention is to return an Object/Entity from the Ajax call and leverage appropriate FTL tags within the required division to retrieve and load the data.

Your help in this matter is greatly appreciated. Thank you!


$.ajax({
    type : "GET",
    cache:false,
    url : "home",
    data : {},
    success : function(mwaCatlogueProfile) {
        alert('data : '+ mwaCatlogueProfile);
        console.log("SUCCESS: ", mwaCatlogueProfile);

        var respContent = "";
        respContent += "<span class='success'> <b>Please find server list:</b>";
        respContent += " </br>";
        respContent += mwaCatlogueProfile.environment + "</span>";

        $("#result").html(respContent);
        wizard.show();
    },

Answer №1

Due to FreeMarker being a server-side technology, the AJAX approach required returning HTML that was processed with FreeMarker on the server side. This means that your templates are all kept on the server side, with some being utilized in regular responses and others in AJAX responses. If the AJAX service you are using is not designed for handling your UI needs, then this method may not be effective, prompting you to explore alternative options such as a JavaScript template engine.

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

Conceal or reveal radio buttons according to information in a table

How can I dynamically add and remove values from a table based on radio button selections? My goal is to add the selected value to the table and hide it from the radio button list, and if the value is deleted from the table, I want to show it back in the r ...

Help with guiding and redirecting links

Here's the code snippet: <script> if(document.location.href.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { document.location.href = 'https://thedomain.com/pages/bestsellers'; } </script& ...

Display only the relevant search results using ng-show in AngularJS

By utilizing the code below, I am able to filter results where all entries are displayed on the page: <body ng-app=""> <div ng-init="friends = [{name:'John', phone:'555-1276'}, {name: ...

Utilizing list comprehension in Python to extract rows from csv and json files

My question pertains to the utilization of list comprehension when reading files in formats like csv or json. The initial code shows a traditional method where values from rows are individually extracted and appended to empty lists: with open("file.csv") a ...

Get the @html.dropdownlistfor filtered for me

Currently, I am working on a project that requires me to filter the options in my second dropdown list based on the selection made in the first dropdown list. While the concept is clear, implementing it becomes tricky as I lack expertise in jQuery or JavaS ...

Exploring the capabilities of the Wordpress REST API for finding and accessing

Looking for some assistance. I currently have a website with woocommerce implemented, using the woocommerce rest api. I'm now trying to create a basic search feature on another website where users can input a query and it will search the woocommerce ...

Using PHP with AJAX for responsive requests to a MySQL database

Here is the code I am working with: <?php $q=$_GET["q"]; require("../db.php"); $res_prop_name = mysql_query("select * from property where prop_short='$q'"); $row_prop_name = mysql_fetch_assoc($res_prop_name); echo $row_prop_name['p ...

Improving User Experience with Yii's Dynamic Form Validation using Ajax

As someone new to Yii, I am facing a challenge with my form. The form is designed as an admin-backend type with several buttons, each linked to a specific action in the controller. My goal is to validate the form elements based on different scenarios and d ...

Converting JAX-RS/JAXB JSON to POJO will only map fields that exist in both the JSON and the POJO, ignoring any additional fields present

In the process of developing an API that must support JSON payloads for PUT/POST requests, which may contain additional fields beyond what is defined in the known POJO. For instance: @XmlRootElement public FruitCounter { int numberOfApples; int num ...

Filtering ng-repeat in AngularJs based on nested data properties

Within my data tracking appointments, I have information on months, weeks, days, and timeslots. When it comes to the listing view, displaying "weeks" is unnecessary (although important for other reports). To eliminate days without appointments, I can use ...

At what point is the rendering process of ng-switch completed?

I am currently utilizing ui-router and facing a challenge in instantiating a widget that requires a DOM element specified by its id as a parameter. The specific DOM element is nested within a <div ng-switch>, and I need to ensure the widget construct ...

Enhancing the performance of jquery selection speed

I am currently working on a code that manipulates the HTML code of an asp.net treeview. Since this code runs frequently, I need to ensure that it executes with maximum speed. I am interested in expanding my knowledge on jQuery selectors and optimizing th ...

React beautiful dnd and Material UI list encountering compatibility problem

I recently encountered an issue while using react beautiful dnd to create a rearrangeable Material UI List. Everything was working correctly, except for the ListItemSecondaryAction within the list. When dragging a list item, the ListItemText and ListItemIc ...

Using the transform property with the scale function causes elements positioned in the bottom right corner to vanish

Issue specific to Google Chrome and Windows 10 I'm currently working on a flipbook that adjusts content size using transform:scale() based on the user's screen size. There is also a zoom feature that allows users to adjust the scale factor. I ha ...

I'm looking to create a post using two mongoose models that are referencing each other. How can I do this effectively?

My goal is to create a Post with the author being the user who created it and have the Post added to the array of posts in the user model that references "Post". Despite searching and watching tutorials, I'm still struggling to understand how to achie ...

Having trouble displaying an image in p5.js on Visual Studio Code

I can't seem to figure out how to load images in visual studio code for p5.js. Can someone help me understand the process of loading images in p5.js? I've set up a new project, but the images I try to load are not displaying correctly. How can I ...

Switch up the Background using .css and jCarousel

I am attempting to create a dynamic gallery using jCarousel and Ajax. The thumbnails are being loaded from a '.txt' file. I am trying to achieve the following effect: When a thumbnail is clicked, the background of the body changes. This action sh ...

Modify the parent scope variable within an Angular directive that contains an isolated scope

Here is a directive implementation: <some-directive key="123"></some-directive> This directive code looks like this: angular.module('app').directive('someDirective', ['someFactory', '$compile', '$ ...

Utilizing either Maps or Objects in Typescript

I'm in the process of developing a simple Pizza Ordering App. The concept is, you select a pizza from a list and it's added to a summary that groups all the selections together. Here's how I want it to appear: Pizza Margarita 2x Pizza Sala ...

Do you need the Tooltip Module for Angular-bootstrap popover to work properly?

"Could it be possible that my popover isn't working because it requires the tooltip module just like the Bootstrap jQuery plugin?" Is this the reason my popover isn't functioning as expected? What exactly is the tooltip module and do I need to i ...