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
- using System;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Script.Serialization;
- namespace TelerikMvcApp1.Controllers
- {
- public class JsonpResult :ActionResult
- {
- public string CallbackFunction { get; set; }
- public Encoding ContentEncoding { get; set; }
- public string ContentType { get; set; }
- public object Data { get; set; }
- public JsonpResult(object data) : this(data, null) { }
- public JsonpResult(object data, string callbackFunction)
- {
- Data = data;
- CallbackFunction = callbackFunction;
- }
- public override void ExecuteResult(ControllerContext context)
- {
- if (context == null) throw new ArgumentNullException("context");
- HttpResponseBase response = context.HttpContext.Response;
- response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/x-javascript" : ContentType;
- if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;
- if (Data != null)
- {
- HttpRequestBase request = context.HttpContext.Request;
- var callback = CallbackFunction ?? request.Params["callback"] ?? "callback";
- #pragma warning disable 0618 // JavaScriptSerializer is no longer obsolete
- var serializer = new JavaScriptSerializer();
- response.Write(string.Format("{0}({1});", callback, serializer.Serialize(Data)));
- #pragma warning restore 0618
- }
- }
- }
- }
Use the above controller code as shown below:
Code Snippet
- public ActionResult UsageJasonP()
- {
- db.Configuration.LazyLoadingEnabled = false;
- db.Configuration.ProxyCreationEnabled = false;
- var waterusages = db.WaterUsages;
- //return Json(waterusages, JsonRequestBehavior.AllowGet);
- return new JsonpResult(waterusages,"receive");
- }
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.