Download CSV from ajax post request - javascript

I'm posting json to my server and I want to return a csv response and have my browser automatically download the csv.
I know there have been some questions on SO exactly about this topic on the past. So far, I've gathered the following solutions:
Put the request into <form> element. This isn't possible because the JSON is nested and too large to be posted as a string.
Save the csv file (get request) onto the server and then have a separate post request to download it. This is not ideal because I don't want to keep a bunch of useless csv files on my server.
Is there a better solution? Can I make a Post request act like a form submission and automatically get the file to start downloading?

Related

can AJAX do anything else than load a JSON file?

I'm want to (or think I need to) use AJAX to accomplish what I intend.
When clicking on a specific link in a list of links, I want to fill the HTML markup below with content of specific subpages. The data is naturally somewhere in the database and actually easily accessible with the CMS's API (I'm using Processwire).
I'm quite new to coding and especially AJAX and all documentation I find online only mention it in combination with a JSON file that would be loaded via AJAX.
However, I don't have a JSON file on the server, that means, according to my understanding, I would need to
store the data I need in a multidimensional php array,
use json_decode to create and then save that JSON-file on the server,
load that file via AJAX and process through more JS.
Let alone keep that JSON-file updated (or create a new one and delete the old one?) since new content will arrive periodically. It seems unnecessarily complicated to me, but what do I know.
There's got to be a better way…
Any help is appreciated.
AJAX is simply a way to make a request to the web server for information.
When you make an AJAX request you ask for a response from a file on a server. So, you can send an AJAX request to a PHP script for-instance.
The PHP script could return anything, JSON is common and very widely used response format, but XML might be another one you've encountered.
So, your request for information is made using AJAX, and the response you get back is JSON.
You don't need to store a JSON file on your server. You just need to make an AJAX request that returns current data in JSON format.
AJAX allows you to do asynchronous HTTP requests.
You can of course ask for a json file, but you can also (for example) call an API.
I suggest you start by reading the the getting started guide for AJAX in MDN:
https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started

How do I send a multipart response in expressjs?

I have an app that displays a list of pictures with titles and descriptions. The app sends a get request to the node server, and I want it to respond with the pictures and the titles/descriptions. In order to send the images at the same time as the other stuff, I think I need to use the multipart/form-data Content-Type. How do I do that? The expressjs docs don't say anything about multipart responses as far as I can tell.
In your case I would send only an initial response with image urls and then generate <img src="image.url"> in the front end.
You don't need multipart here because there will be two separate requests, one for the image metadata, and another when the browser displays the image (downloads the file).
Nevertheless I commented a use case example in which multipart responses seems to be the only way around, and currently cannot find a solution for that one, so thanks for asking.

Request php generated json data via javascript and meanwhile log in php

Currently I have a working javascript/jquery file that makes a request to a php file to retrieve json data. This works great.
Some information is taken from the server and send back to the javascript/jquery.
So:
Request from js to exchange.php ==>
exchange.php access database and gets certain data
<=== response from exchange.php to jquery to send data to js
js uses data to make some changes to the website (changes phonenumber)
This has to be done as fast as possible. But after I have send the data to the js I want the same exchange.php log this data + some userdata to another database. For speed reasons I want to do this after the data is sent to the javascript. Is this possible?
Currently I just call the logging class after I echo'd json_encode(); But does it make a difference? Or will the json be send only after the whole php file has finished running?

xhrpost to download on the fly generated excel file on the server

I have the below requirement.
I have a data grid, which has huge amount of data. I have a download button, when clicked data grid data has to be downloaded to an excel. This data grid data I want to send to server through a rest call using xhrpost. With this data i will construct an excel file on the fly on server side and i will return the response to the client. My client by seeing this response should open popup for downloading the response to save as the file.
I am seeing the following problems here :
the data is huge. so , I can't use window.open way for sending the data in GET format.
I don't have an URL on server which will access file, as i am generating file on the fly and sending response to client.
How will i achieve my use case to download file using xhrpost on the fly, not storing the file anywhere?
Please reply as I am breaking my head how to do this? It will be of great help.
Thanks,
Sreenivas

Server side export to excel in HTML and Javascript

We have a HTML page containing the HTML table which is populated dynamically with content from server. The requirement is to able to export the HTML table to excel in IE as well as Firefox.
We are trying following approach -
The HTML table is first being sent to the server with the content as a POST request using XHR.
at the server side there is HttpServlet which is sending the content back with MIME type as "application/vnd.ms-excel" as a repsonse to the POST request.
At the browser the status of request is monitored and is observed as 200. However we are not getting any prompt from browser for opening the excel.
Appreciate if someone can help with this and throw some light on what we are missing on.
Thanks,
Adish
You have to use form submit instead of XHR if you want a single one POST request to handle that.
If you want to use ajax. The excel file needs to be cached or persisted somewhere. The server returns the url for the excel file in response of xhr post. Then in the success callback use window.open(url) to GET the file, which will prompt for downloading. This will use two requests.
I am not sure if iframe can work around.

Categories

Resources