Posted on

JasonP Implementaion on MVC Controller

 

Please use following link for reference.

 

http://www.snu.net/index.php/2016/11/07/an-asp-net-mvc-jsonp-actionresult-nik-codes/

 

Create this controller code:

JsonpResult.cs

Code Snippet
  1. using System;
  2. using System.Text;
  3. using System.Web;
  4. using System.Web.Mvc;
  5. using System.Web.Script.Serialization;
  6.  
  7. namespace TelerikMvcApp1.Controllers
  8. {
  9.     public class JsonpResult :ActionResult
  10.     {
  11.         public string CallbackFunction { get; set; }
  12.         public Encoding ContentEncoding { get; set; }
  13.         public string ContentType { get; set; }
  14.         public object Data { get; set; }
  15.  
  16.         public JsonpResult(object data) : this(data, null) { }
  17.         public JsonpResult(object data, string callbackFunction)
  18.         {
  19.             Data = data;
  20.             CallbackFunction = callbackFunction;
  21.         }
  22.  
  23.         public override void ExecuteResult(ControllerContext context)
  24.         {
  25.             if (context == null) throw new ArgumentNullException("context");
  26.  
  27.             HttpResponseBase response = context.HttpContext.Response;
  28.  
  29.             response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/x-javascript" : ContentType;
  30.  
  31.             if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;
  32.  
  33.             if (Data != null)
  34.             {
  35.                 HttpRequestBase request = context.HttpContext.Request;
  36.  
  37.                 var callback = CallbackFunction ?? request.Params["callback"] ?? "callback";
  38.  
  39. #pragma warning disable 0618 // JavaScriptSerializer is no longer obsolete
  40.                 var serializer = new JavaScriptSerializer();
  41.                 response.Write(string.Format("{0}({1});", callback, serializer.Serialize(Data)));
  42. #pragma warning restore 0618
  43.             }
  44.         }
  45.     }
  46. }

Use the above controller code as shown below:

Code Snippet
  1.  
  2. public ActionResult UsageJasonP()
  3. {
  4.     db.Configuration.LazyLoadingEnabled = false;
  5.     db.Configuration.ProxyCreationEnabled = false;
  6.     var waterusages = db.WaterUsages;
  7.  
  8.     //return Json(waterusages, JsonRequestBehavior.AllowGet);
  9.     return new JsonpResult(waterusages,"receive");
  10. }

Below is modified Ajax Call from

 

   1: var url = "http://localhost:15214/water/UsageJasonP?callback=?";

   2: $.ajax({

   3:     url: url, dataType: "jsonp", jsonpCallback: "receive", callback: "receive",

   4:     success: function (data, status) {

   5:         //mySurvey.closePopup();

   6:         alert("Success");

   7:         console.log(data);

   8:     },

   9:     error: function (xOptions, textStatus) {

  10:         alert("fails");

  11:     }

  12: });

Now using the above call, you can draw Morris Charts.