C# 在尝试调用控制器时,jQuery一直失败
我试图从jQueryAjax方法调用ASP.NETMVC5中的控制器操作。一切似乎都准备就绪且正确,但我的ajax不断失败。错误返回会一直显示,并且我的页面不会刷新。然而,我的控制器确实被击中了。如果我在C#代码中的特定操作下设置了一个断点,我将尝试按照计划访问所有内容 但是ajax仍然返回一个错误,页面永远不会重定向到 这是我的jQuery脚本C# 在尝试调用控制器时,jQuery一直失败,c#,jquery,asp.net,ajax,asp.net-mvc,C#,Jquery,Asp.net,Ajax,Asp.net Mvc,我试图从jQueryAjax方法调用ASP.NETMVC5中的控制器操作。一切似乎都准备就绪且正确,但我的ajax不断失败。错误返回会一直显示,并且我的页面不会刷新。然而,我的控制器确实被击中了。如果我在C#代码中的特定操作下设置了一个断点,我将尝试按照计划访问所有内容 但是ajax仍然返回一个错误,页面永远不会重定向到 这是我的jQuery脚本 $(document).ready(function () { $('.searchbox').on('search', functi
$(document).ready(function () {
$('.searchbox').on('search', function () {
if ($(this).val() == undefined || $(this).val() == '') {
$.ajax({
type: 'GET',
contentType: 'application/json',
dataType: 'json',
url: '@Url.Action("Index","Company")',
error: function () { alert('error'); }
});
}
});
});
有什么建议吗?如果需要,我可以发布更多代码。我使用的是搜索输入类型,当我清除搜索框时,该类型将触发
谢谢你对我的帮助
这是控制器
[HttpGet]
public ActionResult Index(Int32? resultsPerPage, Int32? startingIndex, Int32? currentPage, String searchExpression)
{
List<ProspectModel> prospects = ProspectManager.LoadProspects(searchExpression);
resultsPerPage = resultsPerPage ?? 25;
startingIndex = startingIndex ?? 0;
currentPage = currentPage ?? 1;
if(!String.IsNullOrWhiteSpace(searchExpression))
{
ViewBag.Pages = 0;
ViewBag.TotalRecords = prospects.Count;
ViewBag.CurrentIndex = 0;
ViewBag.ResultsPerPage = resultsPerPage;
ViewBag.CurrentPage = 1;
ViewBag.LastPageStartingIndex = 1;
ViewBag.SearchExpression = searchExpression;
return View(prospects);
}
List<ProspectModel> model = prospects.GetRange((Int32)startingIndex, (Int32)resultsPerPage);
ViewBag.TotalRecords = prospects.Count;
ViewBag.Pages = (prospects.Count / resultsPerPage) + ((prospects.Count / resultsPerPage) % 2 != 0 ? 1 : 0);
ViewBag.CurrentIndex = startingIndex + resultsPerPage;
ViewBag.ResultsPerPage = resultsPerPage;
ViewBag.CurrentPage = currentPage;
ViewBag.LastPageStartingIndex = ((prospects.Count / resultsPerPage) % 2 == 0 ? prospects.Count - resultsPerPage : prospects.Count - ((prospects.Count / resultsPerPage) % 25));
ViewBag.SearchExpression = null;
return View(model);
}
[HttpGet]
公共操作结果索引(Int32?resultsPerPage、Int32?startingIndex、Int32?currentPage、字符串搜索表达式)
{
List prospects=ProspectManager.LoadProspects(searchExpression);
resultsPerPage=resultsPerPage±25;
起始指数=起始指数??0;
当前页面=当前页面??1;
如果(!String.IsNullOrWhiteSpace(searchExpression))
{
ViewBag.Pages=0;
ViewBag.TotalRecords=prospects.Count;
ViewBag.CurrentIndex=0;
ViewBag.ResultsPerPage=ResultsPerPage;
ViewBag.CurrentPage=1;
ViewBag.LastPageStartingIndex=1;
ViewBag.SearchExpression=SearchExpression;
返回视图(前景);
}
列表模型=prospects.GetRange((Int32)startingIndex,(Int32)resultsPerPage);
ViewBag.TotalRecords=prospects.Count;
ViewBag.Pages=(prospects.Count/resultsPerPage)+(prospects.Count/resultsPerPage)%2!=0?1:0);
ViewBag.CurrentIndex=开始索引+结果每页;
ViewBag.ResultsPerPage=ResultsPerPage;
ViewBag.CurrentPage=当前页面;
ViewBag.LastPageStartingIndex=((prospects.Count/resultsPerPage)%2==0?prospects.Count-resultsPerPage:prospects.Count-((prospects.Count/resultsPerPage)%25));
ViewBag.SearchExpression=null;
返回视图(模型);
}
在MVC中,默认情况下,GET ajax请求被阻止,您必须将其更改为Post或允许GET
return Json("string or model here" ,JsonRequestBehavior.AllowGet);
尝试将jQuery更改为“POST”类型,只需一次,看看是否可以修复它。如果确实如此,那么您可以尝试我提供的代码
您得到的是404,因为您的操作索引没有接受0个参数的路径,您的所有int都设置为null,但您必须至少提供该参数searchExpression
尝试硬编码url而不是Razor,并尝试向其传递某种类型的字符串
$.ajax({
type: 'GET',
contentType: 'application/json',
data: { searchExpression : "test" },
dataType: 'json',
url: '/Company/Index',
success: function(data) { alert('success'); },
error: function () { alert('error'); }
});
另一个答案可能也有很多帮助,我还想建议删除contentType
和dataType
,它们是不需要的,而jQuery在有根据地猜测这些类型应该是什么方面做得很好
$.ajax({
type: 'GET',
data: { searchExpression : "test" },
url: '/Company/Index',
success: function(data) { alert('success'); },
error: function () { alert('error'); }
});
在MVC中,默认情况下会阻止GET ajax请求,您必须将其更改为Post或允许GET
return Json("string or model here" ,JsonRequestBehavior.AllowGet);
尝试将jQuery更改为“POST”类型,只需一次,看看是否可以修复它。如果确实如此,那么您可以尝试我提供的代码
您得到的是404,因为您的操作索引没有接受0个参数的路径,您的所有int都设置为null,但您必须至少提供该参数searchExpression
尝试硬编码url而不是Razor,并尝试向其传递某种类型的字符串
$.ajax({
type: 'GET',
contentType: 'application/json',
data: { searchExpression : "test" },
dataType: 'json',
url: '/Company/Index',
success: function(data) { alert('success'); },
error: function () { alert('error'); }
});
另一个答案可能也有很多帮助,我还想建议删除contentType
和dataType
,它们是不需要的,而jQuery在有根据地猜测这些类型应该是什么方面做得很好
$.ajax({
type: 'GET',
data: { searchExpression : "test" },
url: '/Company/Index',
success: function(data) { alert('success'); },
error: function () { alert('error'); }
});
如果我正确地阅读了您最初的帖子,那么您的意思是,您可以一直访问并通过控制器中的return View()调用,但是您的jQuery AJAX调用说有一个错误 您的视图可能是HTML,这样说对吗?如果是这样的话,因为在AJAX调用中,您已经指定需要JSON,所以jQuery将尝试在将响应提供给您之前将其解析为JSON。这可能是您错误的根本原因 在控制器中,将返回视图替换为:
return JSON(new { Test = "Hello!" }, JsonRequestBehavior.AllowGet);
在AJAX调用的成功处理程序中:
success: function(data) { alert(data.Test); }
如果这样做有效,那么您需要在AJAX中指定要接收HTML,或者从MVC返回JSON模型并在成功函数中处理,这取决于您试图实现的目标。如果我正确阅读了您的初始帖子,那么您的意思是,您可以一直访问并通过返回视图()调用您的控制器,但jQuery AJAX调用表示出现错误 您的视图可能是HTML,这样说对吗?如果是这样的话,因为在AJAX调用中,您已经指定需要JSON,所以jQuery将尝试在将响应提供给您之前将其解析为JSON。这可能是您错误的根本原因 在控制器中,将返回视图替换为:
return JSON(new { Test = "Hello!" }, JsonRequestBehavior.AllowGet);
在AJAX调用的成功处理程序中:
success: function(data) { alert(data.Test); }
如果这样做有效,那么您需要在AJAX中指定要接收的HTML,或者从MVC返回JSON模型并在成功函数中处理,具体取决于您试图实现的目标。如果您想将HTML返回到AJAX调用,请尝试我刚刚玩过的这个示例: 控制器
public class HomeController : Controller
{
public ActionResult Search(
Int32? resultsPerPage,
Int32? startingIndex,
Int32? currentPage,
String searchExpression)
{
return View();
}
}
JavaScript
$.ajax({
type: 'GET',
contentType: 'application/json',
dataType: 'html',
url: '@Url.Action("Search", "Home")',
success : function(data){ alert(data);},
error: function () { alert('error'); }
});
关键是
数据类型
。您需要将其设置为您希望从ajax调用返回的内容类型,在您希望返回HTML的情况下。如果设置不正确,将导致调用error
函数。如果要将HTML返回到ajax调用,请尝试我刚刚玩过的这个示例:
控制器
public class HomeController : Controller
{
public ActionResult Search(
Int32? resultsPerPage,
Int32? startingIndex,
Int32? currentPage,
String searchExpression)
{
return View();
}
}
JavaScript
$.ajax({
type: 'GET',
contentType: 'application/json',
dataType: 'html',
url: '@Url.Action("Search", "Home")',
success : function(data){ alert(data);},
error: function () { alert('error'); }
});
关键是数据类型
。您需要将其设置为th