Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make changes to a page source content variable through Ajax

Is it possible to make changes to the page source content through Ajax loaded by a jsp include in the main jsp ?

If not is it possible to reload that portion of the page alone (the jsp that loads some of the content) and to have a portion of the content in the page source changed ?

Details:

I've a variable called page this var gets its content from a java controller as a map of <String key,map<String key,String value>then it performs multiple actions and adds different params to the map, convert it to JSON and sends it to a jsp.

Recently I wanted to do something different,I want to add a param to the 'page' variable called contentOfThePage this variable gets its content dynamically when the document is fully loaded, after that I perform an Ajax request to the controller to add the new param, but the problem is that the new changes never makes it to the page source unless i reload the page or i navigate to another page and this is causing a lot of trouble as the page source may contain the page content of the previous page!

Any idea on how to avoid this and make changes to the page source (NOT DOM) directly ?

keep in mind that the contents are added dynamically but i need a way to change the page source without impacting the performance by requesting a reload after the ajax request succeeded

like image 695
Ahmad Sanie Avatar asked Aug 26 '16 11:08

Ahmad Sanie


People also ask

How to Update page using AJAX?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.

How do I change the content of a page without reloading it?

To change part of your page from a new request, use Ajax. You can find a lot about it online. That said, using ajax for basic navigation of a simple website is bad taste. Just do a normal navigation.

Which function is used to load a resource in the background in Ajax?

The browser performs a JavaScript call to the Ajax engine. In other words, create an XMLHttpRequest object. In the background, an HTTP request is made to the server and the appropriate data is retrieved. HTML, XML, or JavaScript data is returned to the Ajax engine which then delivers the requested data to the browser.

How do I know if Ajax is working?

In Laravel, we can use $request->ajax() method to check request is ajax or not.


1 Answers

First You want to update some data that is already there after page load

you already have a json so a rest call i assume you call it using ajax

now you added something else that you want to change

Yes it can be done actually

but not with the present set i assume you have a single jsp and trying to change that jsp

well fit whatever you want to change in a panel like a graph or anything else Add a button to top of the panel and on click the button url must be to the rest call so then data will be updated

I too have faced a similar problem with graphs, i needed the graph to give updated data without refreshing the whole page, so i put it inside a panel and wrote a rest controller that gives data for the graph and put a refresh button that calls this rest controller. This let me then update the graph without refreshing the rest of page

like image 119
Raja Avinash Avatar answered Oct 26 '22 09:10

Raja Avinash