This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

unable to export to Excel using Ajax

 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must be missing some fundamental difference between the way a HTTP GET works and the way the jQuery .ajax() get works.

This servlet exports a table of information to Excel and it works fine when this I use this URL:
"localhost:8080/mfgweb/AbsenteeismExcelExport?startdate=20130601&enddate=20130615&numrecords=25"


So why doesn't it work when I use this jQuery code?

By my reasoning I shouldn't have to do anything with the results in the javascript code because the servlet sends the results directly to the PrintWriter. I can see the GET request in Firebug and the response but I never get the dialog box to open or save the Excel file.

What am I missing?

[EDIT] I just found this thread. It seems I'm trying to do the impossible. Back to the drawing board.
 
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo. Ajax doesn't do files.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still puzzled though. My understanding is that Ajax is just a way of forming and sending a request. Once it's sent, that's it. The server forms the response and sends it back to the browser. Is there something unique, such as a header, in the response that says "this response is intended for the Javascript engine, not the HTTP rendering engine"? There must be some way that the browser knows the difference.

I know I'm getting into technical details that aren't really pertinent, but I'm one of those people that has to understand how everything works. Maybe there is a technical specification on Javascript that I need to read?
 
author
Posts: 297
5
Android Firefox Browser Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the browser couldn't tell the difference AJAX wouldn't work, because each response received would just replace the loaded page. If you want to download a file set window.location.href to the download URL and ensure the Content-disposition: attachment header is set at the server side. If you want to guarantee the current page is not replace you cold try dynamically creating an iframe and setting the src on that.
 
author & internet detective
Posts: 40198
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For general understand, take a look at the datatype section of the Ajax jQuery page. It lists the types that can be returned.

Excel is different. It's not text. It's a file. Even your browser doesn't know how to "render" a file. It simply defers to the file system. If Ajax were allowed to download files, you could write a web page that would randomly try to have your machine open downloaded files while you are trying to work. Not good.
 
Your mother is a hamster and your father smells of tiny ads!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic