0

I have a situation where the client (.js) initiates a REST request and it is processed on the server side (.java). After processing, I would like to return a count to the client, which will be shown in a popup box if it is positive. How can such a construction be done? The idea I had was to set a named parameter on the HttpServletResponse object, but even this object is no where in scope in the .js code. Any ideas? I should also clarify that the primary purpose of the REST call is to download a file to the client. Thanks!

shasan
  • 178
  • 2
  • 13
  • You can add an additional attribute in your JSON response. – Luiggi Mendoza Aug 04 '14 at 20:16
  • @bozdoz looks like you had a very similar problem [here](http://stackoverflow.com/questions/22724070). Can you please comment here with what you did? I'm assuming you handle the `response` attribute of the `xmlhttprequest` inside the `onload()` handler. Did you try a `window.open()` on it? – shasan Aug 05 '14 at 16:02

2 Answers2

0

From your question, it seems like you don't have a good general-purpose way of responding to client requests on your server. I'd recommend you decide on a data format you'd like to use for all calls (e.g., JSON, XML, etc.) and stick with that across the board.

Once you've made that decision, you can encode your integer using whatever makes sense in your chosen format. For example, in JSON you might return: {"count":6}.

Andrew Miner
  • 5,587
  • 2
  • 22
  • 26
0

Do you want to send two things to your client - sending a file and also additional data? You haven't mentioned what framework (if any) you are using in backend to do this. You can use response header.

sanj
  • 381
  • 3
  • 3
  • Thanks for your comment sanj. You're right, I'm sending both a file and data. The backend is in Java. There is a `HttpServletResponse.setHeader()` but no getter? Also, even if there is a `getHeader()` or equivalent api, it is still in the .java component and there is no explicit call made to it by the .js component, in order for it to be able to use a returned json/xml. Any hints? – shasan Aug 04 '14 at 21:53
  • I mean you will add the header in your java code and read it in js client. Check this http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript . You will see how to read the headers in javescript. – sanj Aug 04 '14 at 21:59
  • You are correct - auxiliary attribute data is best served up in response headers. I did not have a handle on the response in my js code and was doing a `window.open(/rest-url/)` previously. I reworked it to create an XmlHttpRequest to initiate my rest query and from here things fell into place. – shasan Aug 06 '14 at 23:26