JsonResult shows file download in browser

I am trying to use jquery.Ajax to send data to an ASP.NET MVC2 action method that returns JsonResult. Everything works fine, only when the response is returned to the browser, it is considered as downloading a file, and not passed to the success handler. Here is my code:

JavaScript:

 <script type="text/javascript">
        $(document).ready(function () {
            $("form[action$='CreateEnvelope']").submit(function () {
                $.ajax({
                    url: $(this).attr("action"),
                    type: "POST",
                    data: $(this).serialize(),
                    dataType: "json",
                    success: function (envelopeData) {
                        alert("test");
                    }
                });
            });
            return false;
        });
    </script>

Controller action method:

public JsonResult CreateEnvelope(string envelopeTitle, string envelopeDescription)
    {
        //create an envelope object and return
        return Json(envelope);
    }

If I open the downloaded file, json is exactly what I'm looking for, and the mime type is displayed as application / json. What am I missing for the jquery.ajax call to get the returned json?

+3
source share
2 answers

"return false" . false, JQuery submit, .

<script type="text/javascript">
    $(document).ready(function () {
        $("form[action$='CreateEnvelope']").submit(function () {
            $.ajax({
                url: $(this).attr("action"),
                type: "POST",
                data: $(this).serialize(),
                dataType: "json",
                success: function (envelopeData) {
                    alert("test");
                }
            });
            // IMPORTANT: return false to make sure the normal post doesn't happen!
            return false;
        });
        return false;
    });
</script>

!

+2

ajax, , , , , submit ? ajax. , , , ajax , , json?

0

Source: https://habr.com/ru/post/1739699/


All Articles