MVC returning JSONP

Here is a simple solution, if you don’t want to define an action filter

Client side code using jQuery:

  $.ajax("", { dataType: "jsonp" }).done(function (result) {});

MVC controller action. Returns content result with JavaScript code executing callback function provided with query string. Also sets JavaScript MIME type for response.

 public ContentResult JsonpCall(string callback)
      return Content(String.Format("{0}({1});",
          new JavaScriptSerializer().Serialize(new { a = 1 })),    

Leave a Comment