-1

I'm a beginner in Jquery and Javascript and my knowledge of HTTP protocol is also beginner.

I'm trying to do a simple GET request to a database running on my local machine to retrieve a word document.

$.ajax('http:localhost:databaseURLgoeshere')

I thought it was simple as this but nothing is happening (browser does not download the word document file). I know I am missing something huge and fundamental.

W Jones
  • 1
  • 1
  • 1
    Why would you even want to use AJAX to offer a downloadable file? – JJJ Jan 17 '13 at 19:35
  • 1
    AJAX can't be used to retrieve local files. It only works over the Internet. – Blazemonger Jan 17 '13 at 19:36
  • @Blazemonger Not true, you can use ajax to connect to servers on localhost. He indicates the database is running on his local machine. – AaronLS Jan 17 '13 at 19:36
  • @Juhana In my web app I'm working on we're adding AJAX to it, code snippet above was a small part of it. Blazemonger- It isn't a local file on my machine, I have a database running locally. Can AJAX not retrieve that either? – W Jones Jan 17 '13 at 19:37
  • @WJones What do you want the code to do then? – JJJ Jan 17 '13 at 19:38
  • Did you forget the `//` after `http:` ? – Blazemonger Jan 17 '13 at 19:38
  • @juhana Basically right now I'm just trying to figure out how GET works with AJAX and why my file isn't being retrieved with the function above... I have a test script that has a button wired to a function containing the above code snippet and just seeing if when I click the button, the ajax method is fired and my browswer downloads the word document at the URL location. – W Jones Jan 17 '13 at 19:42
  • so your file is in your database? – wirey00 Jan 17 '13 at 19:43
  • Yes my file is in the database – W Jones Jan 17 '13 at 19:44
  • @WJones You can't initiate a download through AJAX, and I have no idea why you would want to do it. Just use a regular link. – JJJ Jan 17 '13 at 19:44

2 Answers2

1

Edit: The comments bring up a better point which is that this may be an invalid use case.

Perhaps you meant to do $.get() instead of $.ajax(). Also you need to give it direction on what to do on success. ex $.get("url", function() { // do this on success });

http://api.jquery.com/jQuery.get/

Get is just shorthand for $.ajax()

http://api.jquery.com/category/ajax/?rdfrom=http%3A%2F%2Fdocs.jquery.com%2Fmw%2Findex.php%3Ftitle%3DAjax%26redirect%3Dno

jholloman
  • 1,959
  • 14
  • 16
  • Hey thanks for the answer, I have been over these documentations but am still a little confused no the success function. I thought if I requested a GET for a application/msword MIMEtype the browser automatically downloads the file. Is this not true? If not,what would I need to add to my success function to do this? – W Jones Jan 17 '13 at 19:39
  • 2
    Perhaps you should just try "window.location.href = 'http:localhost:databaseURLgoeshere';" instead of an ajax call? – jholloman Jan 17 '13 at 19:42
  • @WJones Ajax gets aren't the same in the browser, although they appear as a regular GET to the server. The browser hands the data to your callback function and lets you decide what to do with it, so the browser doesn't present the file to the user. Hence jholloman's suggestion of setting the window location, which will allow the browser to perform the get and handle the result instead. – AaronLS Jan 17 '13 at 19:50
  • @AaronLS Is there a way in my callback function I could present the file to the user? – W Jones Jan 17 '13 at 19:55
  • @WJones See this link for discussion of that issue: http://stackoverflow.com/questions/4545311/how-to-download-a-file-by-jquery-ajax – AaronLS Jan 17 '13 at 19:56
0

I'm trying to do a simple GET request to a database running on my local machine to retrieve a word document.

This will only work if the database support HTTP protocol access to files, and you have the correct URL to the file, would you be able to do this. Most databases do not have simple file access in this way, but some might. As a test, see if you can put "http://localhost/databaseURLgoeshere" in your browser's URL bar and successfully download the file.

If that doesn't work, then they might also have a web service that is more complicated, in that you send parameters to query the service. We can't help you without without knowing what database web service you are trying to query.

$.ajax('http:localhost:databaseURLgoeshere')

There should be a // after http:, also you are putting http:localhost:databaseURLgoeshere where a port number would go. Instead it would probably be something more like this "http://localhost/databaseURLgoeshere" or "http://localhost:1234/databaseURLgoeshere" where 1234 is whatever port the server is listening on.

Lastly, you can't download files with .ajax, see here for explanation and workarounds: Download a file by jQuery.Ajax

The result of a ajax call will return the data into a callback result, it won't immediately be downloaded. Usually the result of an ajax call is some json, xml, or html. If it were HTML for example, you would then reight javascript to insert that result wherever you want it in the page. My point being, the ajax call just retrieves the file/xml/html/data etc., it doesn't actually make it appear to the user. You have to write additional javascript to place it on the page.

Community
  • 1
  • 1
AaronLS
  • 37,329
  • 20
  • 143
  • 202
  • Aaron, Thanks for the answer. My address for the database file is correct and I am able to pull up my file when I go to it. (When I visit the address the borrower downloads the word document). Am i Missng something here? When I just visit the link I send a GET request to the address right? (which lets me download the file). If I did this with AJAX wouldn't it be the same? – W Jones Jan 17 '13 at 19:51
  • 1
    Result of an ajax call is passed to your callback so that you are in complete control of how it is handled. The usual stuff browser does to present the file to the user, doesn't happen. The reason for this is if it returned some HTML in your ajax request, you would then write javascript in your callback to insert that piece of HTML into the page where it needed to go. So AJAX is designed that way to put you in control. As others have mentioned, maybe instead of doing ajax, you can try setting windows.location – AaronLS Jan 17 '13 at 19:55
  • @WJones another option, not knowing what you are doing, is just use jquery to insert a link onto the page that points to that URL. I'm not sure what is triggering this download, but if you are wiring up to the click event of some link, you could always just have the href of the link set dynamically using javascript, so that the user clicking the file starts the download. To see this, just manually make a link `Test` and click it to see that it will download the file. Using jquery you can create elements or change the HREF dynamically. – AaronLS Jan 17 '13 at 20:11