JSONPResult.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace EMIS.Web.Controls
  7. {
  8. public class JSONPResult : JsonResult
  9. {
  10. public JSONPResult()
  11. {
  12. JsonRequestBehavior = JsonRequestBehavior.AllowGet;
  13. }
  14. public string Callback { get; set; }
  15. ///<summary>
  16. ///对操作结果进行处理
  17. ///</summary>
  18. ///<paramname="context"></param>
  19. public override void ExecuteResult(ControllerContext context)
  20. {
  21. var httpContext = context.HttpContext;
  22. var callBack = Callback;
  23. if (string.IsNullOrWhiteSpace(callBack))
  24. callBack = httpContext.Request["callback"]; //获得客户端提交的回调函数名称
  25. // 返回客户端定义的回调函数
  26. httpContext.Response.Write(callBack + "(");
  27. httpContext.Response.Write(Data); //Data 是服务器返回的数据
  28. httpContext.Response.Write(");"); //将函数输出给客户端,由客户端执行
  29. }
  30. }
  31. }