Json return invokes file save dialog in asp.net mvc

I integrate jquery fullcalendar in my application. Here is the code I'm using:

in index.aspx:

<script type="text/javascript">
   $(document).ready(function() {
       $('#calendar').fullCalendar({
           events: "/Scheduler/CalendarData"
       });
   });  
</script>

<div id="calendar">
</div>

Here is the code for the scheduler / CalendarData:

public ActionResult CalendarData()
    {

        IList<CalendarDTO> tasksList = new List<CalendarDTO>();

        tasksList.Add(new CalendarDTO
        {
            id = 1,
            title = "Google search",
            start = ToUnixTimespan(DateTime.Now),
            end = ToUnixTimespan(DateTime.Now.AddHours(4)),
            url = "www.google.com"
        });
        tasksList.Add(new CalendarDTO
        {
            id = 1,
            title = "Bing search",
            start = ToUnixTimespan(DateTime.Now.AddDays(1)),
            end = ToUnixTimespan(DateTime.Now.AddDays(1).AddHours(4)),
            url = "www.bing.com"
        });

        return Json(tasksList,JsonRequestBehavior.AllowGet);
    }

    private long ToUnixTimespan(DateTime date)
    {
        TimeSpan tspan = date.ToUniversalTime().Subtract(
        new DateTime(1970, 1, 1, 0, 0, 0));

        return (long)Math.Truncate(tspan.TotalSeconds);
    }

    public ActionResult Index()
    {
        return View("Index");
    }

I also have the following code inside the main tag in site.master:

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" />
<link href="<%= Url.Content("~/Content/jquery-ui-1.7.2.custom.css") %>" rel="stylesheet" type="text/css" />
<link href="~Perspectiva/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~Perspectiva/Content/fullcalendar.css" rel="stylesheet" type="text/css" />
<script src="~Perspectiva/Scripts/jquery-1.4.2.js" type="text/javascript"></script>
<script src="~Perspectiva/Scripts/fullcalendar.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>

All I did was pretty much copied from http://szahariev.blogspot.com/2009/08/jquery-fullcalendar-and-aspnet-mvc.html

When navigating through / scheduler / calendardata, I get a prompt to save json data whose contents are exactly what I created in the CalendarData function.

What do I need to do to display the page correctly?

Thanks in advance,

Eran

: Rune Franci CalendarData.aspx, index.aspx. :

  • /scheduler/calendardata .
  • /scheduler/index, Visual : Microsoft JScript error: . .. $ ().ready(()...) .
+3
6

JSonResult ActionResult.

public JSonResult CalendarData()
{
    IList<CalendarDTO> tasksList = new List<CalendarDTO>();

    tasksList.Add(new CalendarDTO
    {
        id = 1,
        title = "Google search",
        start = ToUnixTimespan(DateTime.Now),
        end = ToUnixTimespan(DateTime.Now.AddHours(4)),
        url = "www.google.com"
    });
    tasksList.Add(new CalendarDTO
    {
        id = 1,
        title = "Bing search",
        start = ToUnixTimespan(DateTime.Now.AddDays(1)),
        end = ToUnixTimespan(DateTime.Now.AddDays(1).AddHours(4)),
        url = "www.bing.com"
    });

    return Json(tasksList, JsonRequestBehavior.AllowGet);
}
+1

URL-, , Index.aspx URL-, CalendarData(). , - "/scheduler". "/scheduler/calenderData", Json .

0

/scheduler/calendarData , Content-Type: application/json. , , , .

/scheduler/index, - , ( Content-Type: text/html).

, CalendarData , :

  • , Content-Type: application/json
  • , Fiddler, HTTP
0

JSonResult ActionResult.

0

, ActionResult, View ...? , - ... , , ;

Homecontroller;

public ActionResult Index()
    {
        ViewData["Message"] = "Whatever!!! just work!";

        return View();
    }

Global, , GetData, :

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);
0

The only way I got this to work without the end user going through crazy things is to change the content type to "text / html" in the return.

I tried all of the above suggestions, as well as several others.

Hell, I even tried this .

0
source

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


All Articles