Using the HTTP Accept Header from JavaScript
- 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 r开发者_JS百科equest 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.)
Don't think so. I think you should use an parameter instead.
精彩评论