MVC之Ajax

简单介绍

 

msdn介绍

 

前台页面:

ajaxOptions定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@{
    var ajaxOptions = new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "wrap",
        OnBegin = "waitingDialog",
        OnComplete = "closeWaitingDialog",
        OnSuccess = "onRequestSuccess",
        OnFailure = "onRequestFailed",
        HttpMethod = "POST"
    };
    var ajaxHtmlAttr = ajaxOptions.ToUnobtrusiveHtmlAttributes();
    var virtualPath = HttpRuntime.AppDomainAppVirtualPath;
    var clientPath = Request.Url.PathAndQuery;
    if (virtualPath != "/")
    {
        clientPath = clientPath.Substring(virtualPath.Length);
    }
    clientPath = clientPath.ToLower();
}

  

event定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function onRequestFailed(ajaxContext) {
      if (ajaxContext.statusCode == 0 || ajaxContext.statusCode == 401 || ajaxContext.statusText == "error") {
          window.top.location = loginUrl;
      }
      else {
          //alert("服务器端错误,请稍后再试。");
      }
      if (ajaxContext.statusText) {
          console.log("failed request,server response:" + ajaxContext.statusText);
      }
      window.top.location = loginUrl;
 
  }
 
 
 
 
 
 
 
 
  var currentRequestUrl = null;
  var inRequestState = false;
  function onRequestSuccess() {
      if (currentRequestUrl) {
          inRequestState = true;
          var currentMenuName = $(".submenu li.current").text().trim();
          currentRequestUrl = currentRequestUrl.replace("&X-Requested-With=XMLHttpRequest", "");
          History.pushState({ activeMenu: currentMenuName, title: "@ViewBag.Title", url: currentRequestUrl }, $("#wrap").find("title").text(), currentRequestUrl);
      }
  }
 
  History.Adapter.bind(window, 'statechange', function () { // Note: We are using statechange instead of popstate
      if (inRequestState) {
          inRequestState = false;
          return;
      }
      var state = History.getState(); // Note: We are using History.getState() instead of event.state
      if (state) {
          document.title = state.title;
          var mn = state.data.activeMenu;
          $.ajax({
              type: "POST",
              url: state.url,
              success: function (data) {
                  $("#wrap").html(data);
                  switchMenu(mn);
              },
              failure: function (errMsg) {
                  $("#wrap").html("<p>服务器繁忙,请稍后再试.</p>");
                  console.log(errMsg);
              }
          });
 
      }
  });   
 
 
 
 
 
 
 
 
function waitingDialog(waiting, reqOptions) {
      if (reqOptions && reqOptions.url) {
          currentRequestUrl = reqOptions.url;
      }
      else {
          currentRequestUrl = null;
      }
      $.Dialog({
          title: '加载中',
          content: '<div style="text-align: center">加载中,请稍候...</div>',
          overlay: true,
          overlayClickClose: false,
          width: 330,
          height: 100,
          padding: 20,
          sysButtons: {
              btnClose: false
          }
      });
  }
 
 
 
 
 
 
  function closeWaitingDialog() {
      try {
          $.Dialog.close();
          $.each($('.metro.window-overlay'), function (i, v) {
              $(this).remove();
          });
          $.each($('.ui-helper-hidden-accessible'), function (i, v) {
              $(this).remove();
          });
      } catch (e) {
          $.each($('.metro.window-overlay'), function (i, v) {
              $(this).remove();
          });
          $.each($('.ui-helper-hidden-accessible'), function (i, v) {
              $(this).remove();
          });
      }
  }

  

posted @   PanPan003  阅读(192)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示