WagoL
WagoL

Reputation: 107

done not executed ajax

Im having this piece of code. Where I use this Action to get trigger an export that downloads an excel file. Which works perfectly when I type the link and argument in my browser, the file gets downloaded.

But I want to call this from an ajaxified context and this is where it all gets wrong.

<script type="text/javascript">
    function exportPerson(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var action = '@Url.Action("ExportContactAlarmList", "Contact")';
    $.ajax({
        url: action + '/' + dataItem.Id,
        type: "POST",
        done: function(response) {
            var dataURI = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" +
                kendo.util.encodeBase64(response);
            kendo.saveAs({
                dataURI: dataURI,
                fileName: "PersonExport.xlsx",
                proxyURL: "@Url.Action("Save", "Home")"
            });
        }
    });
} 
</script>

I'm kind of stuck because the done method never gets executed. And I don't know why.

These are my responses from my headers I get back. Header responses

Everything looks good, no errors in the console.

Upvotes: 0

Views: 66

Answers (2)

Gaspar Teixeira
Gaspar Teixeira

Reputation: 1267

Normally I use $.ajax with these functions:

  1. success = A function to be called if the request succeeds
  2. error = A function to be called if the request fails
  3. complete = A function to be called when the request finishes (after success and error callbacks are executed). I also use it to stop the loading bar.
  4. beforeSend = A pre-request callback function. Even used to start loading bar.

So I would suggest you to use this:

success: function(response) {...

ref: http://api.jquery.com/jquery.ajax/

I am not sure, but there are some restrictions to download files using XMLHttpRequest. Maybe if you define the header before... See accepts settings form $.ajax and dataType.

Good luck!

Upvotes: 1

Suraj Gavhane
Suraj Gavhane

Reputation: 176

try this

$.ajax({
        url: action + '/' + dataItem.Id,
        type: "POST",
        success: function(response) {
            var dataURI = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" +
                kendo.util.encodeBase64(response);
            kendo.saveAs({
                dataURI: dataURI,
                fileName: "PersonExport.xlsx",
                proxyURL: "@Url.Action("Save", "Home")"
            });
        }
  });

or

$.ajax({
       url: action + '/' + dataItem.Id,
        type: "POST"    
}).done(function(response) {
        var dataURI = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" +
        kendo.util.encodeBase64(response);
        kendo.saveAs({
                dataURI: dataURI,
                fileName: "PersonExport.xlsx",
                proxyURL: "@Url.Action("Save", "Home")"
        });
});

Upvotes: 0

Related Questions