Using Thymeleaf within Javascript code to generate a URL?

Here is my question:

  1. The project's base URL is :
  2. The test page URL is :, and on this page, I have included a JavaScript file called datatable.packer.js using the following code:

<script type="text/javascript" th:src="@{/resources/js/datatable.packer.js}"></script>

In the mentioned datatable.packer.js file, there is an AJAX call with the URL "/api/test", as defined below:

var url="/api/test"; 
  1. However, when accessing the page, the AJAX call URL gets parsed to "" instead of the expected URL "". Even if the URL definition is changed to "api/test", it will parse to "" ...

Any suggestions for solving this issue? How can I correctly use URLs in JavaScript code? Thank you very much for your assistance.

Answer №1

One approach is to create a JavaScript block where you specify the URL. It may not be the most elegant solution, but it does get the job done.

<script th:inline="javascript">
    /*<![CDATA[*/
    apiUrl = "http://servername/sampelProjectName/api/test";
   /*]]>*/
</script>

By following this method, your JavaScript code should have access to that variable.

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

What is the process of utilizing an npm package as a plain JavaScript library through require and module export?

Hey there, I'm a bit unsure if I'm on the right track with my question, but here it goes: What steps do I need to take to modify a node.js package for use as a standalone embedded script/library in HTML? How can I invoke a class constructor in ...

Challenges encountered in d3.js when parsing through data sets

For the past two days, I have been struggling to solve this error and I'm at a loss. Every time I try to add data to the visualization, I encounter the following error: Uncaught TypeError: Cannot read property 'length' of undefined It seem ...

The lack of a defined theme in the makeStyles for @mui/styles sets it apart from @material-ui/core

Struggling to update my material-ui from version 4.11 to version 5 and running into problems with themes. import { createTheme } from '@mui/material/styles'; import { ThemeProvider, StyledEngineProvider, } from '@mui/material/styles&apo ...

When Custom Route Provider is not a valid function

This question has been revised since this morning. I am working with two distinct modules, 'admin' and 'authorization'. Within the provider block of the authorization module, I utilize $routeProvider to add an identical route to all ro ...

Issue with component not updating despite waiting for react context

I'm troubleshooting why the page isn't re-rendering once isFetchingData is set to false. I have a useEffect in the context and expect it to trigger a re-render when isFetchingData changes. Any suggestions? Refreshing the page displays the data, ...

Prohibited Nested Attributes in Embedded Mongoid Document when Using AJAX

I'm facing an issue while attempting to submit a document and an embedded document through an Ajax call. I keep encountering an "Unpermitted parameter" exception. This is how my model is defined: class UserForecast ... embeds_many :time_entries ...

Tips for creating a scale animation using HTML5 Canvas

I am currently developing a canvas whiteboard tool and I have reached the stage where I am focusing on implementing the Zoom In and Zoom Out feature. While the functionality is working fine, I would like to enhance it with smooth animations for scaling. H ...

Maintain the current application state within a modal until the subsequent click using Jquery

Due to the challenges faced with drag and drop functionality on slow mobile devices, I am considering implementing a modal approach for moving elements from one place to another. Here is how it would work: When a user clicks on an item, it is visually ma ...

Using JQuery to reverse the action of hiding an image

Having some trouble with my Javascript and JQuery skills, so bear with me. I've set up a gallery of images where users can drag and drop them into a designated drop zone. Everything works fine when dragging an image from the gallery to the drop zone a ...

Develop a carousel component using Vue.js

I'm currently working on a dashboard that needs to display cards, but I'm running into an issue. I want only four cards visible at a time, and when you click the arrow, it should move just one step to the next card. For example, if cards 1-4 are ...

Error: Attempting to access an undefined property ('call') on Next.js is causing a TypeError

Exploring the realms of Next.js and Typescript for a new project! My goal is to utilize next.js with typescript and tailwind CSS by simply entering this command: npx create-next-app -e with-tailwindcss my-project Smooth sailing until I hit a snag trying t ...

Creating visually appealing tables in React.js

Having trouble with table rendering in React. The buttons in the table cell are not styled properly and do not center within their div. Additionally, the table border is cut off where there are buttons or empty headers. Need help figuring out what's g ...

Get your hands on a PDF containing server node and vue.js

I am facing an issue with creating a secure download link for a PDF file on the server. When clicking on the link, the file is not being downloaded properly. Ensuring that the PDF link remains hidden from the client but still allows for downloading direct ...

How can I create a hover effect animation in React?

I am looking to replicate the menu item underline animation demonstrated in this example. In jQuery, I would easily achieve this by obtaining the left position and width of a menu item, then using stop.animation on hover. Attempting this animation with R ...

How about inputting some text into a field?

Below is the code snippet for your reference: <div class="col-xs-12 col-sm-9"> <input id="Shipping_FirstName" name="firstname" ng-model="userOrder.Shipping.FirstName" type="text" class="form-control nsg-form--input ng-pristine ng-untouc ...

Exploring the process of sending props via the render method in Vue.js and observing their behavior

Struggling with passing a prop from a parent component down to a child created using CreateElement/render in vue.js and then watching it. Here is the parent component: Vue.component('my-drawing', MyDrawing) new Vue({ el: '#drawing' ...

``There is an issue with the onsubmit property that prevents the opening of

I have encountered an issue with my forms that has me stumped. Despite searching online for help, I can't seem to figure out why my implementation is not working as intended. The concept is straightforward. I've embedded a form within a JSP page ...

Is there a method to prevent the repetition of this loop?

I have a question regarding my React app development. I am currently using useEffect to loop through six items every time, even when only one item has changed. How can I optimize this so that only the variable that was changed in the reducer function is ...

How do I fix the scroll top issue caused by Jquery fadeIn?

Currently facing an issue with the jQuery fadeIn (or fadeOut) method. I have constructed an article rotator which is functioning properly. However, a problem arises when the page is scrolled to the bottom and the articles rotate. The fadeIn (or fadeOut) me ...

Utilizing mapped data to display numerous Material-UI Dialog elements

On my table, I have a list of users displayed. Each user has a button in their row to delete them. Clicking the delete button triggers a Material-UI Dialog to confirm. An issue arises where 3 dialogs are being rendered due to mapping, and the last dialog ...