1
  • I have a web service that performs a database search. It accepts both GET and POST requests, and can return data in either JSON, CSV, or HTML format based on the HTTP Accept header.
  • I have a web page that makes an Ajax request to this web service, and displays the search results.
  • I have been asked to add a button to this page that will allow the user to save the data in CSV format.

Earlier this year, someone was in the same boat, and got the response

You cannot do it using javascript capabilities, since javascript has no permission to write on client machine, instead you can send request to server to create csv file and send it back to client.

So I added a button that does

window.open("MyWebService.cgi?" + theSameQueryStringIPassedInTheAjaxCall),

which opens the HTML version in a new browser tab. I want the CSV version. Is there a way I could pass an Accept: text/csv HTTP header? (I know how to do it with XMLHttpRequest and setRequestHeader, but that doesn't help me.)

Community
  • 1
  • 1
dan04
  • 77,360
  • 20
  • 153
  • 184
  • 1
    Wouldn't it be easier to add a parameter to the querystring? (e.g. format=csv) – davehauser Sep 01 '10 at 19:43
  • 1
    I'd prefer not to reimplement something that's part of the HTTP protocol. But I could easily do that if I have to. – dan04 Sep 01 '10 at 19:52

1 Answers1

1

Don't think so. I think you should use an parameter instead.

Maurice Perry
  • 31,563
  • 8
  • 67
  • 95