Partitioning a JSP page accessed through an ajax call
I have a page in which I am making an ajax call, which in turn gets forwarded to a jsp page and returns a table constructed in this jsp.
Till now this jsp used to return an html wh开发者_运维百科ich the original page(table) used to append in a particular div. But now, we have a requirement that this jsp along with the table, returns some other info about the metadat of the query.
With this change, I would ideally not like to change the existing behaviour of some clients where they are just appending the html. But then, is there a way, in which the new pages which make this call can have both the html table(which can be appended) as well as metadata returned (which can be processed).
let me know if the question isn't clear enough.
Thanks!
The easiest and least destructive change would be to send it as response header.
In the servlet you can use HttpServletResponse#setHeader()
to set a response header:
response.setHeader("X-Metadata", metadata);
// ...
(using a header name prefixed with X-
is recommended for custom headers)
In JS you can use XMLHttpRequest#getResponseHeader()
to get a response header:
var metadata = xhr.getResponseHeader('X-Metadata');
// ...
You can even set some JSON string in there so that (de)serialization is easy.
精彩评论