Jedi Master Spooky
Jedi Master Spooky

Reputation: 5819

.getJSON produce a Download of a File ASP.NET MVC

I code a .getJSon, It does the job but I get IE to asked to download the file. Here is the code

<script type="text/javascript">
$(function() {
    $('#id').click(function() {
        var dateReport = "01/01/2009";

        $.getJSON('/Report/SendReport', { date: dateReport},
                function(response) {
                    if (response.result == "OK") {
                        $('#OKSendReport').toggle();
                        $('#OKSendReport').html("OK");
                    }
                });
    });
});

The code in the controller is

 public ActionResult SendReport(string date) {
        //DO Stuff

        return new JsonResult {
            Data = new { result = "OK" }
        };
    }

Any ideas?

Upvotes: 1

Views: 1287

Answers (2)

Ryan Lanciaux
Ryan Lanciaux

Reputation: 5976

Hmm Are you sure that the Javascript is being triggered in that instance? What is HTML type is #ID? If the JavaScript is not getting called and your controller is trying to route to the json result, I think that could be your problem.

Upvotes: 0

Luca Matteis
Luca Matteis

Reputation: 29267

Try adding the event.preventDefault(); on the click event:

$(function() {
    $('#id').click(function(event) {
        var dateReport = "01/01/2009";

        event.preventDefault(); // added this

        $.getJSON('/Report/SendReport', { date: dateReport},
                function(response) {
                    if (response.result == "OK") {
                        $('#OKSendReport').toggle();
                        $('#OKSendReport').html("OK");
                    }
                });
    });
});

Upvotes: 2

Related Questions