1

I am trying to make an Ajax call to a Struts 2 action method and then displaying the value of a variable in the action class. Even though the method is getting called, I am unable to see the value of the variable. The getters and setters of the variable are in place in the action class.

My action method:

public String testMethod(){
    //Set value of variable "myVariable"
}

The Ajax method:

$.ajax({
    type: "POST",
    url: "testmethod",    //the declaration in struts.xml
    success: function(){
       //how to access value of myVariable here
}
});

in the success callback of the Ajax call in the JSP page, I am trying to access it via the <s:property> but it does not seem to be working.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Gaurav Sood
  • 680
  • 4
  • 17
  • 38

1 Answers1

0

In the success callback function you should pass the results via parameter, then use this parameter to assign the value of your variable. For example

$.ajax({
    type: "POST",
    url: '<s:url action="testmethod"/>',    //the declaration in struts.xml
    success: function(data){
       //how to access value of myVariable here
       var myVariable = data;
}
});  

To return the data via Ajax call you could see this answer.

Roman C
  • 49,761
  • 33
  • 66
  • 176