Tips for transferring JSON data from wicket to JavaScript

In my Java Script page, I have a function that calls Wicket Ajax:

function callWicketPage() {  
                wicketAjaxGet(
                urlCallback,
                function() {alert('success'); },
                function() { alert('failed');
                }); 
            }

And in the Wicket page, I have the following code:

   final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() { 
    @Override
    public void renderHead(IHeaderResponse ihr) {
        super.renderHead(ihr);
        ihr.renderJavascript("var urlCallback = '" + this.getCallbackUrl() + "';", "insertedjavascript");
    } 
        protected void respond(final AjaxRequestTarget target) { 
        }
    };
    add(behave);

I am looking to send a JSON response from the Wicket page to JavaScript. How can I achieve this?

Answer №1

I highly recommend utilizing a ResourceReference instead in order to avoid the need for read/write access to the page instance and to facilitate easier client side script access.

If you prefer to stick with your current behavior-based approach:

class JsonBehavior extends AbstractDefaultAjaxBehavior {

    @Override
    protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
        super.updateAjaxAttributes(attributes);

        // Remember to specify the channel type (default: queue)
        attributes.setChannel(new AjaxChannel("json"));
    }

    @Override
    protected void respond(AjaxRequestTarget target) {
        RequestCycle rc = RequestCycle.get();

        rc.replaceAllRequestHandlers(new TextRequestHandler("application/json", "UTF-8", "{'json':'content'}"));
    }
}

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

Converting hexadecimal to binary using Javascript or Typescript before writing a file on an Android or iOS device

Hey everyone! I'm facing a puzzling issue and I can't seem to figure out why it's happening. I need to download a file that is stored in hex format, so I have to first read it as hex, convert it to binary, and then write it onto an Android/ ...

How to retrieve the ID of a table column using jQuery in HTML

I am facing an issue with a HTML table that consists of only one row and one column: <table id="backlog"> <colgroup> <col width="200"></colgroup> <tbody> <tr><td id="91" tabindex="0" class="mark">BackLog&l ...

Unlocking hidden gridview column values with the power of jQuery

Within my gridview control, there are 4 columns, with one column being invisible which contains the Email Address information. <asp:GridView id='gridData' runat='server'> <Columns> <asp:TemplateField> ...

Develop a JavaScript application that contains a collection of strings, and efficiently sorts them with a time complexity of O(nlog n)

Seeking assistance in developing a JavaScript program that contains an array of strings and must sort them efficiently in O(nlog n) time. Grateful for any guidance... ...

Create a new variable to activate a function within the parent component in Vue

I want to invoke a function in the parent component from its child component. In my Vue project, I have designed a reusable component that can be used across multiple pages. After the function is executed in this component, I aim to call a specific functi ...

Is there an undocumented property lurking in the depths of the Webpack server

The document specifies that Options that work with webpack-dev-middleware will have a key symbol next to them. I couldn't find any information on "cookieDomainRewrite" but when I tried using it, it worked. Any insights? Or suggestions on how I ...

Issues with Braintree webhooks and CSRF protection causing malfunction

I have successfully set up recurring payments with Braintree and everything is functioning properly. Below is an example of my code: app.post("/create_customer", function (req, res) { var customerRequest = { firstName: req.body.first_name, lastN ...

Tips on transferring information from one function to another in React.js

Can anyone help me figure out how to pass data from a radio button to a common function in a React project? I've been able to achieve this with a submit button, but struggling with the radio button. Here's what I have so far: App.js /* Extracti ...

Conceal object after a brief pause

Why does the element hide immediately instead of slowly fading out? I can't figure out why it doesn't first display the "P" tag and then gradually hide it. Please help me solve this issue. var step = 0.1; var delay = 90000; var displayMe = fun ...

Discovering the correct way of utilizing Vuelidate's minValue and maxValue functionality

I am having trouble figuring out how to validate for values greater than 0. When using minValue(0), it also accepts the value 0. On the other hand, if I use minValue(1), it does not accept any decimal between 0 and 1. Furthermore, I am facing an issue wit ...

HTML and JavaScript code to desaturate or grayscale image rollovers without the need for additional image duplicates or sprites

Is there a way to achieve grayscale to color image rollover effects without duplicating images or using sprites? I'm looking for an alternative technique that can accomplish this. Any suggestions on how to implement it? ...

Connect jQuery's resizable controls

Check out this jSFiddle. I am attempting to add event handlers for resizing $('oWrapper_'+num). However, the resizing does not occur. This is because $('#oWrapper_'+num) has not been added to the dom at the time of execution, so the se ...

Tips for sending an array of input field values through an Ajax request

In my current web form, there is a dynamic option that adds rows with the same attributes. These rows need to be submitted through an Ajax call to my PHP for insertion into the database. Access: <tr> <td><input type"text" name="access[nam ...

Changing the host in every URL within a string using node.js

I am working with a string that contains URLs with IP addresses: { "auth" : { "login" : "http://123.123.11.22:85/auth/signin", "resetpass" : "http://123.123.22.33:85/auth/resetpass", "profile" : "http://123.123.33.44:85/auth/profile" ...

Real-time Data Stream and Navigation Bar Location

Utilizing the EventSource API, I am pulling data from a MySQL database and showcasing it on a website. Everything is running smoothly as planned, but my goal is to exhibit the data in a fixed height div, with the scrollbar constantly positioned at the bott ...

Tips for retrieving data from functions that make HTTP requests in node.js?

Calling the getStockValue() function from another JavaScript file looks like this: var stock=require("./stockdata"); var data = stock.getStockValue()); The data variable currently only contains "-BEGIN-". My goal is to retrieve the body object returned ...

Updating Variables in Azure DevOps Code Prior to DeploymentORModifying Variables

Can Azure DevOps automate code updates upon release? For instance, in my React app, I have a setting file with export const ISPROD = false. I want Azure DevOps to modify this value to true before building and deploying the app. Is this achievable? Please ...

Prevent clicking on a specific column in a table using Jquery

Attempting to utilize Jquery for clicking on a table row in order to navigate to a new page. However, encountering an issue with the last column containing a button that redirects to a new page when clicked on the edge. Is there a way to disable the oncl ...

Image Animation Using a Rotational Control (HTML/JavaScript/...)

I am interested in achieving a specific effect with a series of images that transition from dark to light. My idea is to have a dial or slider underneath or over the frame so that when it is moved to the left, the black image is displayed, and when moved t ...

Including jQuery in an Angular project generated with JHipster

I am facing a challenge with integrating jQuery into my Jhipster Angular project as a newcomer to Jhipster and Angular. My goal is to customize the theme and appearance of the default Jhipster application, so I obtained a theme that uses a combination of ...