.NET/MVC JSON response opens up dialog box to save

2019-07-21 04:19发布

I am trying to submit a form via jquery's ajax method in my .net mvc application. I've set the dataType to json and the contentType to "application/json; charset=utf-8". In my controller action, I'm returning a JsonResult.

For some reason, the JSON response doesn't get handled correctly and instead I get a dialog box to save a file with the JSON object inside of it.

$(document).ready(function() {

    $("#editPageContentForm").submit(function() {

        $.ajax(
  {
      type: "POST",
      dataType: "json",
      url: $("#editPageContentForm").attr("action"),
      contentType: "application/json; charset=utf-8",
      data: { ID: $("#id").val(), small_title: $("#small_title").val(), big_title: $("#big_title").val(), body: $("#body").val(), subheading: $("#subheading").val() }, 

      success: function(result) {
          alert('hi');
      },

      error: function(req, status, error) {
          alert("Sorry! We could not receive your feedback at this time.");
      }
  }
 );
 })

In my controller action, I have something akin to:

public JsonResult Edit(int id, string small_title, string big_title, string subheading, string body)
{
     return Json(new {success = true, message = "success"});
}

Why is the response not coming back as JSON?

7条回答
贼婆χ
2楼-- · 2019-07-21 05:07

Try throwing a return false; at the end of your $("#editPageContentForm").submit() function.

查看更多
登录 后发表回答