Can bytes(pdf) be opened from an ajax call - ajax

My controller returns a pdf file using an ajax call. It's clear that using
window.open(response.reponseText)
is obviously not gonna help on success return
It'd be helpful if my controller that spits out the pdf file didn't take a List of object as a parameter. That's why I have to use an ajax call to call the controller. I can't use an iFrame for this exact reason as well. I found that using ajax I could send a json object. I can't do this using iframe.
Is there a way to open the returned file or even pop up a message box to download the file.

Related

Uploading a file stored in browser memory via ajax

Assume there are no cross-domain issues with the ajax calls to the server(s).
First, I am making an ajax call to grab the contents of a binary file (docx) from the server. The contents are then stored in the browser memory.
Next, I need to make another ajax call to the server to upload the file contents along with other form fields. The page may already contains the form, or the js can dynamically create the form (which would be hidden), or I can use the FormData object in the js code.
I can't figure out how to take file contents that are in memory and include them in a form POST call to upload the file along with the other form inputs. Any idea how to do this?
I thought perhaps using the FileReader object I could do this, as I have seen where you can use FileReader.readAsBinaryString() on a blob or binary data, then have the onloadend trigger the form submit.
I would prefer to use jquery, but that is not necessary.

Scala Play - calling controller method from view doesn't load a new page

I display information, gather some user's input, and call a controller method using ajax/java script call from the view. However, instead of displaying a new/different html page, the controller method returns back to the original html page that I called jscript from.
Is this because of ajax call? How do I make sure that controller method would display the html file it's supposed to, instead of staying on old html page?
It sounds to me like what you want to do is to open a different url than the one you are on. To do that you do not use ajax. Instead try using javascript to change window.location to tell the browser to go to the url (or 'call your controller'). It looks like this:
window.location.assign("/your/controller/url"); // or
window.location = "/your/controller/url";
There is more to read about it here: https://developer.mozilla.org/en-US/docs/Web/API/Window.location
You can think of ajax as a separate little browser that lets you call the web server but letting you handle what comes back with javascript rather than always showing it to the user. So if you want to do the call with ajax but get the response HTML into your page, you are responsible for taking that response and inserting it into the DOM.

Why URL Data does not shows for GET method in ajax?

As we know, GET method sends data via URL. We can use both GET and POST method in ajax. My question is, why can't we see the data in URL when we use ajax with a GET method?
AJAX call is not visible through url.
Try to use firebug extension of firefox.
In firebug, you will find all request in console.
When you fire off your ajax request like this:
The Chrome network tab will show new line for this transaction:
If you click on it, it will show you all the details:
If you'd like more information on constructing your own querystrings, look at this other question: How to build query string with Javascript

In Rails how do I use ajax? where do I put my Ajax code?

How do I get data from the database, without refreshing the page?
I dont even know where to start...
In Rails you will need to implement a page that pulls data from the DB and shows the data in whatever format you prefer, plaintext/json/xml. There's no asynchronous stuff in this part.
Ajax will be happening in frontend, the javascript part, that asynchronous connects to the webpage that you made above. jQuery's $.ajax will save you a lot of time.
You'll need to make your AJAX call from either an onload.js or a page specific js file (example.js). You'll then need to run a $.get or $.post call to the URL of the data.
Here is a pretty detailed tutorial: http://www.tutorialspoint.com/ruby-on-rails/rails-and-ajax.htm

Handling an ASP.NET MVC FileResult returned in an (jQuery) Ajax call

Goal:
I want to let my users download a file on my webpage. I want a new window to open and the file to be either displayed or downloaded there.
My implementation:
This file however, first has to be generated on the server-side, which might take a while. When the user clicks the button to download the file, I do an ajax call and show a waiting animation until I get a response. The controller action that handles the call will generate the file (PDF) and return a FileResult. Now in the succes function of my ajax call back in javascript, I get the file data.
Problem: I have no Idea what I'm supposed to do with this data to get it to the user.
Workaround:
Right now I use a workaround where I do not return the file in the ajax call, but store it in session. In the succes function I do window.open("/controller/getPDFFromSession") which will download the file. However, I prefer not to use the session for these kind of things.
Thanks in advance.
Problem: I have no Idea what I'm supposed to do with this data to get it to the user.
You shouldn't use AJAX for downloading files for this reason. You could do the following:
The user clicks on the download button
Using javascript you show some progress image informing him that he will have to wait
Using javascript you generate and inject a hidden iframe into the DOM having its src property pointing to the controller action supposed to generate the file
Once the iframe is loaded you could hide the progress image

Resources