Link to home
Start Free TrialLog in
Avatar of puneet kumar
puneet kumar

asked on

i reload the page with success message when ajax response will get from java controller

How can i reload the page with success message when ajax response will get from java controller .
Case - i am sending an ajax request to java controller it is successfully going to java controller i am setting a response in controller and revert it back to jsp . while success i want to reload the page with success message which i already set in java controller.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

You do not refresh the page. Instead, you inject the response to an element.

Let's say your jsp page outputs, "This is the response".  And you want to put the response in a div#results

https://jsfiddle.net/cw2g5f49/

HTML
<div id="results"></div>

Open in new window

JQUERY
  var response="This is the response";
  $('#results').html(response);

Open in new window


Putting that to your ajax https://api.jquery.com/jquery.ajax/ request, you simply inject your result in the done or success function.
$.ajax({
  method: "POST",
  url: "page.jsp",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    //alert( "Data Saved: " + msg );
     $('#results').html(msg );
  });

Open in new window


This assumes your result is either html or text. If it is outputting json, you will need to parse the json in your done/success function first.
Avatar of puneet kumar
puneet kumar

ASKER

yes but if my page is not refreshed on success how can page communicate with database and show the latest data.hope you understand.
This part
.done(function( msg ) {
    //alert( "Data Saved: " + msg );
     $('#results').html(msg );
  });

Open in new window

is extracting the data from your jsp page after it has completed. Then places the results on the page.

Do a test. When you do use the browser dev tools by hitting F12. Look in the Network tab and go to the xhr tab. Run the page and you can see your jsp page appear.  Click on the page name in dev tools. Once you do that, you will have new tabs for Headers, Preview and Response .The header tab shows what is being sent to your controller. The other two tabs show the response that your done/success function grabs.

No need to refresh the screen.
There is a good tutorial on https://www.codecademy.com/learn/learn-jquery

Take an hour or so to go through it. If you are not familiar with javascript, go through that first https://www.codecademy.com/learn/introduction-to-javascript  It will be well worth spending part of a day on to better understand what is going on here.
yes but if my page is not refreshed on success how can page communicate with database and show the latest data.hope you understand.
Then the question is - why are you using AJAX? If you need a server side refresh then posting by AJAX, getting a response and then refreshing the page to re-render does not make sense. Might as well post directly to the controller and get the rendered page back.

If you really want to do it this way then
// AJAX request (assuming jQuery)
$.ajax({
  ...,
  dataType: 'JSON' // Assume a return type of JSON
}).then(function(resp) {
   // Assumes a JSON structure { status: true } [You did not specify so have to improvise]
   if (resp.status) {
     // Do the redirect to the target page
     window.location.href = "https://path/to/your/target_page.jsp";
   }
});

Open in new window

Hi julian Thanx for for your support please let me know i want to reload the page with previous data . your function works refresh with no previous data.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You guys are extremely helpful thanx a ton for support.