Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How can I call ajax when I click a button in aem programmatically?

chung_yonge1684
Level 4
Level 4

Hello Everyone,

Can I ask a question ?

I want to know how to call ajax when I click a button in aem programmatically?

Can you show me the source codes for it ?

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Your selectors are not correct, please se id selector like below:

var val1=$('#v1').val();

var val2=$('#v2').val();

View solution in original post

3 Replies
chung_yonge1684
Level 4
Level 4

Thank you for reply.

I have another question.

When I click [Post to servlet],

1653470_pastedImage_0.png

val1 is undefined.

1653471_pastedImage_3.png

test.html

        <form>

                    Value1:<input type="text" id="v1" name="v1" value="AL" />

                    Value2:<input type="text" id="v2" name="v2" value="OA" />

                    <input type="submit" class="perform" value="Post to servlet!!!"/>

                </form>

test.js

$(".perform" ).click(function() {

    var val1=$('v1').val();

    var val2=$('v2').val();

    alert("val1 : " + val1);

    alert("val2 : " + val2);

    //Use JQuery AJAX request to post data to a Sling Servlet

    $.ajax({

        type: 'POST',

        url:'/bin/imgcounter',

        data:{'value1' : val1,'value2' : val2},          //passing values to servlet

        success: function(msg){

            //Success logic here(The response from servlet)

        }

    });

    location.reload();

});

Can you check my source what is wrong?

Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Your selectors are not correct, please se id selector like below:

var val1=$('#v1').val();

var val2=$('#v2').val();

View solution in original post