如何使用AJAX调用来调用View

发布于 2024-12-11 11:30:50 字数 2342 浏览 0 评论 0原文

我想使用 ASP.net MVC2.0 的 AJAX 调用来调用视图,但它不能正常工作。这是一个 AJAX 方法

$.ajax({
          type: 'POST',
          url: '../Inventoryhealth/IHView?mac=' + val + '&name=' + val2 + '#fragment-3',
          //url: '../Chart/CreateChart2?chartType=Column&a=null',
          success: function (result) {
          var res = result;
          if (res != null && res == "1")
              alert('System information can\'t be retrieved');
          }
      });

,但如果我使用它,它工作得很好

location.href = "../Inventoryhealth/IHView?mac=" + val + "&name=" + val2 + "#fragment-3";

这是视图代码

public ActionResult IHView(String mac, string name)
    {
          try
          {
              ViewData["PollTime"] = new ClientConfigurationService().getPollTime() * 60000;
              SystemInventoryService sis = new SystemInventoryService();
              SystemInformation systemInfo = new SystemInformation();
              systemInfo = sis.getSystemInventory(mac);
              systemInfo.ChartRefreshInterval = getInterval();
              systemInfo.OName = name;
              bool MoreCores = true;
               if (Convert.ToInt16(systemInfo.NumberOfCores) < 2)
              {
                  systemInfo.Core1UsageDetail = "0";
                  systemInfo.Core2UsageDetail = "0";
                  MoreCores = false;
              }

              Add(systemInfo.ProcessorLoadPercentage,   systemInfo.MemoryTotalVirtualMemorySize, systemInfo.MemoryFreeVirtualMemory, systemInfo.DrivesSize, systemInfo.DrivesTotalFreeSpace, MoreCores, systemInfo.Core1UsageDetail, systemInfo.Core2UsageDetail);

            var AC = new ActiveClient();
            AC.ClientMac = mac;
            if (db.ActiveClients.Count() > 0)
            {
                db.DeleteObject(db.ActiveClients.First());
            }
            db.AddToActiveClients(AC);
            db.SaveChanges();

            if (systemInfo != null)
            {
                return View(systemInfo);
            }
            else
            {
                // If Healh and status can't be retrieved                    
               // Response.Redirect("../Inventoryhealth/InventoryIndex?error=1");
                return Content("1");
            }

        }

你知道吗?

I want to call view using AJAX call with ASP.net MVC2.0 but it doesn't work fine. This is an AJAX method

$.ajax({
          type: 'POST',
          url: '../Inventoryhealth/IHView?mac=' + val + '&name=' + val2 + '#fragment-3',
          //url: '../Chart/CreateChart2?chartType=Column&a=null',
          success: function (result) {
          var res = result;
          if (res != null && res == "1")
              alert('System information can\'t be retrieved');
          }
      });

but if I use this it works fine

location.href = "../Inventoryhealth/IHView?mac=" + val + "&name=" + val2 + "#fragment-3";

This is view code

public ActionResult IHView(String mac, string name)
    {
          try
          {
              ViewData["PollTime"] = new ClientConfigurationService().getPollTime() * 60000;
              SystemInventoryService sis = new SystemInventoryService();
              SystemInformation systemInfo = new SystemInformation();
              systemInfo = sis.getSystemInventory(mac);
              systemInfo.ChartRefreshInterval = getInterval();
              systemInfo.OName = name;
              bool MoreCores = true;
               if (Convert.ToInt16(systemInfo.NumberOfCores) < 2)
              {
                  systemInfo.Core1UsageDetail = "0";
                  systemInfo.Core2UsageDetail = "0";
                  MoreCores = false;
              }

              Add(systemInfo.ProcessorLoadPercentage,   systemInfo.MemoryTotalVirtualMemorySize, systemInfo.MemoryFreeVirtualMemory, systemInfo.DrivesSize, systemInfo.DrivesTotalFreeSpace, MoreCores, systemInfo.Core1UsageDetail, systemInfo.Core2UsageDetail);

            var AC = new ActiveClient();
            AC.ClientMac = mac;
            if (db.ActiveClients.Count() > 0)
            {
                db.DeleteObject(db.ActiveClients.First());
            }
            db.AddToActiveClients(AC);
            db.SaveChanges();

            if (systemInfo != null)
            {
                return View(systemInfo);
            }
            else
            {
                // If Healh and status can't be retrieved                    
               // Response.Redirect("../Inventoryhealth/InventoryIndex?error=1");
                return Content("1");
            }

        }

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深者入戏 2024-12-18 11:30:50

我认为不要使用Post,而是使用Get方法。通话中没有数据选项,因此这意味着您不需要发布。我希望它能够改变帖子的获取方式。

I think instead of using Post, use Get method. There is no data option in the call so it means you don't need posting. I hope it will work on changing Post to get.

寂寞美少年 2024-12-18 11:30:50

您可以尝试在指定 URL 时删除“..”吗?那应该可以正常工作。您还可以使用 firebug 查看正在从服务器请求 URL。

Can you try removing ".." while specifying the URL. THat should work fine. You could also use firebug to see that URL is being requested from the server.

余罪 2024-12-18 11:30:50

使用 jQuery 的 $.get 而不是较低级别的 $.ajax 函数。您想要执行 http GET 而不是在此原因中发布帖子。

尝试这样的事情:

$.get('../Inventoryhealth/IHView?mac=' + val + '&name=' + val2 + '#fragment-3', function(data){
//do something on success
alert(data);
});

jQuery Get

另外..我注意到你的网址中有一个哈希值(#)。您需要对其进行 url 编码,否则将返回 404。

Use jQuery's $.get rather than the lower level $.ajax function. You want to perform an http GET not a post in this cause.

Try something like this:

$.get('../Inventoryhealth/IHView?mac=' + val + '&name=' + val2 + '#fragment-3', function(data){
//do something on success
alert(data);
});

jQuery Get

Also.. I noticed you have a hash in your url (#). You will need to url encode this or it will return a 404.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文