如何使用AJAX调用来调用View
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为不要使用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.
您可以尝试在指定 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.
使用 jQuery 的 $.get 而不是较低级别的 $.ajax 函数。您想要执行 http GET 而不是在此原因中发布帖子。
尝试这样的事情:
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:
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.