HtmlUnit AJAX response is partial HTML. Its failing parsing - java

The response of an AJAX response in HtmlUnit is a single div, which contains a table of data.
The response also contains a small JS script.
The problem is that HtmlUnit is trying to parse the response as a complete HTML. So, it expects that snippet to have all JS libraries like jQuery.
Is there a way to parse the snippet in the context of the parent page which fired the AJAX?
Alternatively, it would be ok if I just got the response as plain text. But, the request has to be within the session, along with all the Html headers intact.

I guess the AJAX url can't be used for getPage. Instead, I loaded the outer page once, and triggered a script on that page, which loaded the AJAX response into a div.
String jsCommand = "$('#results_box').load( '"+ pageLink +"',"+ formdata +");";
parentPage.executeJavaScript(jsCommand);

Related

Parsing html with page hash navigation

I have to parse an html page. I get it via url such as "https://..bla-bla../final_page/#hash-command". But JSoup gets page without hash-command, just final_page, but it's not the page i want to parse. How can I make a request to get the right responce? (I also tried to use okHttp to establish a connection, but it wasn't make any result).

Session Attribute Not getting updated in .gsp Page after Ajax Call

I am using session attributes in my .gsp page like below:
<g:set var="maxValue" value="${session?.MY_VAR}"/>
I am making an AJax call from Javascript to the Groovy controller ( MyController.groovy ) and setting it like below:
session.setAttribute("MY_VAR", "abc");
After Ajax call returns back to the GSP. The value of session variable is not updated. It still stores the old value from the previous load.
Any ideas on how to solve this ?
Your GSP is generated on the server and the resulting HTML is sent to the browser. Your Ajax call does not force a page refresh, so the GSP along with the line below is not reevaluated and the variable maxValue is not changed.
<g:set var="maxValue" value="${session?.MY_VAR}"/>
You should consider render as JSON in your controller to return maxValue and using JavaScript to update the DOM after the Ajax response is received. See render for more info.

Javascript not executing on JSP forward action

Hi I have one jsp page and I have to update two html select boxes with the data of an inf file. On selecting the first combo(initially also not loading the second combo), the second will have to change accordingly. Since java script variable is not accessible from java, I had sent the value of first combo to an another jsp by ajax. And then return it to first jsp itself.Plaese help me.
Maybe I'm getting it wrong but looking at your code it seems that you are doing AJAX request on selectbox change event, but you are not doing anything with the response.
So basically what happens is you are doing an initial render to your jsp and everything is fine. Now you are changing selectbox option and AJAX request is sent to dummy.jsp. dummy.jsp is receiving it and redirecting the maker of the request to User.jsp. User.jsp-s content is sent back but nothing is done on the client side AJAX call with this response.
What you should do is have your logic at dummy.jsp(or even better at some servlet) which based on your AJAX call request parameters returns for example a JSON with data that you can process in jQuery.ajax success event. In there you can use this JSON to readjust your selectboxes and other data with javascript.
Check the examples at the end of jQuery.ajax manual: http://api.jquery.com/jquery.ajax/

DOJO AJAX - How to change the whole JSP content?

I am using Dojo library. How can I replace all the content of my jsp/html content. I am trying to dynamically reload my page when a data is updated.
Here is my dojo code:
function reloadPage() {
var thisUrl = '/CBS/a/customer/' + customerId + '/profile';
dojo.xhrGet({
url: thisUrl,
load: function (data) {
document.body.innerHTML = data;
},
error: function (data, ioArgs){
document.body.innerHTML = "unknown error";
}
});
}
The server returns a complete html code including the html tags. The data variable holds all the html tags. In my code I did document.body.innerHTML = data;which is wrong because the content of body is replaced by a whole html page. It looks like ajax is working because its updated dynamically but my buttons are not working anymore. Please help.
Why do you use AJAX if you want to update the whole page? Purpose of using of AJAX - update part of content(page), not updating the whole page. If you want to update the whole page, may be it will be more appropriate to use reload?

HTML page as AJAX response?

Can I send an entire HTML page with an AJAX response? If so, how to render that HTML page and what are the pros and cons doing that. The reason why I am asking this question is I found out that if we use response.sendRedirect("index.html") as a reply to an AJAX request in servlet we get the indx.html as AJAX response XML.
Your question doesn't make much sense and I can't quite tell what you're asking - the response to an ajax request will be whatever the server sends back. It could be plain text, XML, HTML, a fragment of an HTML/XML document etc. What you can do with depends on your script. If you're using a library like jQuery, what happens on the client side and what you can do with the response can also depend on how the library interprets the response (Is it a script? It it HTML/XML or JSON?).
if we use response.sendRedirect("index.html") as a reply to ajax request in servlet we get the indx.html as ajax response xml. Can some one pls explain this
An ajax request will behave much like a 'regular' HTTP request. So when you send back a redirect from your server (HTTPServletResponse#sendRedirect), the browser follows the redirect just like it would for any other request. If your ajax request was to a resource that required HTTP BASIC authentication, you'd see a prompt to login, just like you would if you visited the URL directly in a new browser window.
If you want to send HTML as a response, because you want to update divs, tables or other elements, but still want to use the same css or javascript files then it can make sense.
What you can do is to just send it as plain/text back to the javascript function, it can then take that and put it into the inner html element that you want to replace, but, don't do this if you want to replace the entire page, then doing what you want is pointless.
When you make the http request for your ajax call, it has its own response stream, and so when you redirect you are just telling the browser to have that http request go to index.html, as #no.good.at.coding mentioned.
If I get your question, you just want to know whether you could return whole entire HTML with AJAX and know the pro and cons.
The short answer to your question is yes, you could return the entire HTML page with your AJAX response as AJAX is just an http request to the server.
Why would you want to get the entire HTML? That's confusing to me and that is the part that I am not clear about. If you want to want to render the entire HTML (including tags like html, body, etc?), you might as well open it as a new page instead of calling it via Ajax.
If you are saying that you only want to get fragments of HTML to populate a placeholder in your page via AJAX then this is an acceptable practice. jQuery even provides load() function (http://api.jquery.com/load/) to make that task easy for you to use (check the section Loading Page Fragments).
Using this method, you could populate the placeholder using the HTML Fragments that is dictated by your server logic (i.e when the login fails or succeed) including the one via server redirect in your question.

Categories