Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return JSONResult without the browser prompting to save the file?
    primarykey
    data
    text
    <p><strong>EDIT: I've long since moved beyond VS2008, and I haven't had any issues with returning JSON results using MVC 3+. I'd like to mark this question as obsolete, or something to that effect. Perhaps someone will still find value in this and the answers given, but I can't mark them as "correct" since I have no way of testing them.</strong></p> <p>I am new to MVC, and am trying to make a simple site work. I'm starting to wonder if it is really worth it...I could have had this site up and running with "old-school" ASP.Net two or three times already...but that is beside the point ;-)</p> <p>How can I get my controller to return a JSONResult without the browser prompting me to save the response as a file? Here is the JavaScript that calls the action:</p> <pre><code>$("select#PageId").change(function() { var id = $("#PageId &gt; option:selected").attr("value"); $.getJSON('FindCategories/', { PageId: id }, function(data) { if (data.length &gt; 0) { var options = ''; for (c in data) { var cat = data[c]; options += "&lt;option value='" + cat.CategoryId + "'&gt;" + cat.CategoryName + "&lt;/option&gt;"; } $("#CategoryId").removeAttr('disabled').html(options); } else { $("#CategoryId").attr('disabled', true).html(''); } }); }); </code></pre> <p>Here is my controller action:</p> <pre><code>Function GetCategoriesByPage(ByVal PageId As Integer) As JsonResult Dim categories As List(Of Models.WebCategoryLite) = _service.ListCategoriesByPageId(PageId) Dim res As New JsonResult res.Data = categories Return res End Function </code></pre> <p>Fiddler shows me that the JSON is being returned to the browser:</p> <pre><code>HTTP/1.1 200 OK Server: ASP.NET Development Server/9.0.0.0 Date: Mon, 24 Aug 2009 19:43:53 GMT X-AspNet-Version: 2.0.50727 X-AspNetMvc-Version: 1.0 Cache-Control: private Content-Type: application/json; charset=utf-8 Content-Length: 246 Connection: Close [{"CategoryID":1,"CategoryName":"Sample Category"},{"CategoryID":2,"CategoryName":"Another Sample"},{"CategoryID":3,"CategoryName":"Yet Another Sample"}] </code></pre> <p>No matter what browser I try this with, I get a "save file as" prompt.</p> <p>I am running this from inside the Visual Studio 2008 IDE. What do I have to do to make this work, both in the IDE and from IIS?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload