0

I not able to display the json data on jsp. When i checked for console logs in browser I am able to see the json content in console. Can anyone help me with ajax code to display the data?

Web.xml content

<struts> 
<constant name="struts.devMode" value="true" /> 
<package name="default" extends="json-default" namespace="/">       
<action name="getCronDetails" class="myaction">             
<result name="SUCCESS">/schedule.jsp</result>       
</action>   
</package> 
</struts>

Action class content:

List<emp> myList=new ArrayList<emp>() ;
//setter getter method for myList 

public String getCronDetails (){        
myList = fetchData() ;          
return "SUCCESS"; 
}

I want to display myList in table format which is of type emp. Can any one help me with ajax code?

Ashok bhor
  • 21
  • 6
  • I'm not clear on what you're actually having a problem with since you don't include any of your JS code you're using to try to display the data. – Dave Newton Mar 28 '18 at 14:02

1 Answers1

0

I suggest using jQuery for the ajax calls. In the html you need an element to trigger to call, as an example I am using a button.

The button should trigger the js function that calls the action. In the example I have added callbacks for disabeling the button and showing a spinner (I left them commented, as the spinner is not there).

You get the ajax response in the response variable. After you get the response you can update the DOM with it's contents.

$(document).ready({
 
 $("#getButton").click(function(event){
  $.ajax({
        type: "GET",
        url: "getCronDetails",
        dataType: "json",
        beforeSend: function(){
       //Disable send button and start a spinner
        },
        success: function(response, status, jqXHR){
         //console.log("Ajax Success!");
         if(typeof response !== 'object'){
          response = JSON.parse(response);
      }
         //doSomethingWithThe(response);
        },
        error: function(jqXHR, status, error){
         //console.log('Ajax Error');
         //showError('Ajax Error: ' + error );
        },
        complete: function(){
         //Enable Submit Button and stop spinner
        }

  });
 
 
 });
 

});
 
...
<body>
 <!-- A button to trigger the ajax call -->
 <button id="getButton">Get Cron Details</button>
 
 <!-- Include jQuery -->
 <script
     src="https://code.jquery.com/jquery-3.3.1.min.js"
     integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
     crossorigin="anonymous"></script>
</body>
...
Juan
  • 5,525
  • 2
  • 15
  • 26