Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

URL for post request to servlet changes

Avatar

Level 5

I'm using sling:resourceType = "granite/ui/components/coral/foundation/form" for creating a form. In this I've mentioned the action = "/bin/servlets/servlet-name". There is one textfield which has name = "userName". I have not mentioned any method. When I hit submit button, the GET request is sent to servlet forming the following url : http://localhost:4502/bin/servlets/ranosys-osgi-config?.%2FuserName=nikita . That means the value is passed as the parameter in the requestURL. But when I add method="post", the parameter is not passed into the URL resulting into the following link: http://localhost:4502/bin/servlets/ranosys-osgi-config which results into the null values for the servlet variables. Is there a way that the parameter can be added automatically in the URL for the POST request too?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@nikita24tailor You can by adding the parameter value in the hidden field 

<input type='hidden' name='<prop-name>' id='<prop-name>' value="${numero}" />

Thanks

View solution in original post

7 Replies

Avatar

Community Advisor

@nikita24tailor For sending the parameters to the post request try out the sample code as below:

$.ajax({
        url: '<url-path>',
        type: 'POST',
        data: { field1: "hello", field2 : "hello2"} ,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            alert(response.status);
        },
        error: function () {
            alert("error");
        }
    }); 

here field1 and field2 are the two parameters that are being passed from frontend to the servlet.

 

Thanks.

Avatar

Level 5

Yes, I can use this but sling:resourceType = "granite/ui/components/coral/foundation/form" already provides way to post data without any ajax through the use of method and action properties. I want to utilise that.

 

Avatar

Correct answer by
Community Advisor

@nikita24tailor You can by adding the parameter value in the hidden field 

<input type='hidden' name='<prop-name>' id='<prop-name>' value="${numero}" />

Thanks

Avatar

Employee Advisor

When we make post request  typically it has relevant information in the body of the request.

Why do you want to add parameter with POST request?

Avatar

Level 5

As I've mentioned earlier, I'm using sling:resourceType = "granite/ui/components/coral/foundation/form" for rendering my form. The documentation which I'm following is https://developer.adobe.com/experience-manager/reference-materials/6-4/granite-ui/api/jcr_root/libs/... . It says that form has already these properties, i.e., method and action and the values will be post to the servlet. But when I try to access the values in servlet using request.getParameter(userName), it shows null. But if the values are passed as parameters, then I can read and access them in servlet. 

But as you are saying that we can access these form values without passing parameters to the servlet, please let me know how to do that.

Avatar

Community Advisor

Hi @nikita24tailor 

 

I understand that you are using a Form component and want to make a POST request for submission of the form. May I know which form component are you using, is it core form container component or foundation form component ?

Avatar

Level 5

No, I'm not using any component. I'm extending tools menu and in that I'm creating a node of type form.