views:

238

answers:

3

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?

A: 

What is the response/content of /Report/SendReport ?

+1  A: 

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");
                    }
                });
    });
});
Luca Matteis
I get the following error now: Object does not support this method
Jedi Master Spooky
its works ok in safari with your modification
Jedi Master Spooky
You have to add the `event` arugment in the function callback.
Luca Matteis
A: 

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.

Ryan Lanciaux