MVC Controller Return Content vs Return Json Ajax

50,648

Using Content(Foo); sends a response that doesn't have the mime type header. This happens because you're not setting ContentType when using this overload. When the Content-Type is not set, jQuery will try to guess the content type. When that happens, whether it can successfully guess or not depends on the actual content and underlying browser. See here:

dataType (default: Intelligent Guess (xml, json, script, or html))

Json(...) on the other hand explicitly sets the content type to "application/json" so jQuery knows exactly what to treat the content as.

You can get consistent result from Content if you use the 2nd overload and specify a ContentType:

return Content(Foo, "application/json"); // or "application/xml" if you're sending XML

But if you're always dealing with JSON, then prefer using JsonResult

return Json(Foo, JsonRequestBehavior.AllowGet); 
Share:
50,648
silencedmessage
Author by

silencedmessage

I'm currently working full time using ASP.Net WebAPI, Angular 2, Oracle and Mongo. I have a passion for automating tedious tasks and believe strongly in DevOps. I also have an interest in mobile development using Xamarin.

Updated on September 23, 2020

Comments

  • silencedmessage
    silencedmessage over 3 years

    In MVC, why does returning Content sometimes fail in the Ajax callback, while returning Json works, even for simple string objects?

    Even when it fails, the data is still available if you were to access it in the always callback...

    Update:

    When I set the contentType in the ajax call to text/xml the response will no longer enter the error message.

    AJAX:

    $.ajax({
        cache: false,
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        url: "/MyController/GetFooString",
        data: { },
        success: function (data) {
            alert(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Ajax Failed!!!");
        }
    }); // end ajax call
    

    Controller Action That Fails (Sometimes)

    Even when it fails, the data is still available.

    public ActionResult GetFooString()
    {
        String Foo = "This is my foo string.";
        return Content(Foo);
    } // end GetFooString
    

    Controller Action That Always Works

    public ActionResult GetFooString()
    {
        String Foo = "This is my foo string.";
        return Json(Foo, JsonRequestBehavior.AllowGet);
    } // end GetFooString
    
  • emerson.marini
    emerson.marini over 9 years
    +1. But if the OP will always expect JSON, wouldn't be better to just change ActionResult to JsonResult?
  • Mrchief
    Mrchief over 9 years
    @MelanciaUK: If one always expects JSON, then yes. But I see your point. Updating my answer...
  • emerson.marini
    emerson.marini over 9 years
    Thanks for clarifying. I was in doubt.
  • silencedmessage
    silencedmessage over 9 years
    In this case, I would always be expecting JSON, but the part that I was truly missing was the mime type and the differences between ContentResult and JsonResult.