0

I make an ajax call as

var formData = new FormData();
formData.append('name', 'John');
formData.append('company', 'ABC');

$.ajax({
  url: url,
  data: formData,
  processData: false,
  contentType: false,
  success: function(data){
     window.location.href = data.URL;   // data does not have URL attribute.
  },
  error: function(err){}
});

Here, I should ideally get an excel file download as response.

However, the

success: function(data){

}

Here the data that i get contains a bunch of encoded values which I can see in the browser console. How can I get the download url here.

user544079
  • 13,851
  • 35
  • 102
  • 160
  • You've prepared your ajax options as though you want to upload a file. I think you should have regular ajax options and also include `dataType:'json'` and make sure your server is returning a `JSON`, then you can grab the `file URL` as you're trying to do. **NO NEED TO USE FormData()**. – PeterKA Jun 26 '14 at 21:20
  • What, exactly, are you trying to do? – PeterKA Jun 26 '14 at 21:25
  • Actually, I have to send the formData to the service, the service sends me the data. Inside success: function(data){}, when i do console.log(data) I can see an encoded output in the browser. But I don't have the data.URL – user544079 Jun 26 '14 at 21:37
  • I see. Is it because the service is not sending it? I really don't know what you're trying to do but maybe you could take a look at the `table2CSV` jQuery plugin; it may give you an idea or two .... just a shot in the dark :) – PeterKA Jun 26 '14 at 21:56

2 Answers2

0

AJAX is meant to be used for sending/retrieving data, so obviously you will get data in success function.

You should make POST with ordinary form and make sure that server is setting the right header.

<form method="post" action="/path/to/excel/file">
    ...
</form>

After form make POST, and server supply data and HEADER if your browser does not support viewing of excel files the download will start.

Nikolay
  • 44
  • 2
0

You can't download files with ajax that would be saved on you hardrive for security reasons. There is a previous question covering the same problem: Download a file by jQuery.Ajax

Community
  • 1
  • 1
Adrian Forsius
  • 1,421
  • 2
  • 20
  • 27
  • I think he's just getting back by ajax just the URL of the file, not the file itself. – PeterKA Jun 26 '14 at 21:18
  • But then the download url should be the same as he was specifying the ajax call AKA variable url?, or am I overlooking something, please clarify. – Adrian Forsius Jun 26 '14 at 21:20
  • I think there's a confusion of variables - `data` in success not to be confused with `formData` sent to server. He's not even sending a URL to the server but expects to receive one from the server. Maybe I have it wrong but that's how it appears to me. – PeterKA Jun 26 '14 at 21:22
  • Yes, that is correct. I send formData to the server, the server sends me the response in the success: function(data){}. However,when I do console.log(data), i can see encoded values in the browser. I cannot get any data.URL – user544079 Jun 26 '14 at 21:39
  • So you requested with a URL and you get a response, what URL is this response-URL suppose to represent? – Adrian Forsius Jun 26 '14 at 21:46