C# 加载页面时Ajax调用未命中控制器
我有一个ajax调用,它在另一个页面上似乎工作得很好,但是当我在这个页面上调用它时,它就不工作了 CSHTML页面C# 加载页面时Ajax调用未命中控制器,c#,jquery,ajax,model-view-controller,C#,Jquery,Ajax,Model View Controller,我有一个ajax调用,它在另一个页面上似乎工作得很好,但是当我在这个页面上调用它时,它就不工作了 CSHTML页面 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <script src="~/Script/jquery-1.12.2.min.js"></script> <s
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Script/jquery-1.12.2.min.js"></script>
<script src="~/Script/moment.js"></script>
<script type="text/javascript">
function qs(key) {
key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
var match = location.search.match(new RegExp("[?&]" + key + "=([^&]+)(&|$)"));
return match && decodeURIComponent(match[1].replace(/\+/g, " "));
}
function initialiseForms() {
$.ajax({
type: 'POST',
url:'@Url.Action("Index", "Home")',
contentType: 'application/json; charset=utf-8',
error: function (response) { alert(JSON.stringify(response)); }
}).success(function (response) {
alert(JSON.stringify(response));
});
}
</script>
</head>
<body onload="initialiseForms()">
<h1 id="titleText"></h1>
<form id="evolveFormsForm" method="post" target="_self" action="Test">
<input type="hidden" id="formPackEventData" name="formPackEventData" value="" />
<input type="hidden" id="selectedTemplateCodes" name="selectedTemplateCodes" value="" />
<input type="hidden" name="logonMethod" value="automatic" />
</form>
</body>
</html>
这个警报(JSON.stringify(response))代码>在响应中起作用,但未命中断点,警报仅显示一个空字符串
干杯您的ajax
不应该是这样吗
$.ajax({
type: 'POST',
url:'@Url.Action("Index", "Home")',
contentType: 'application/json; charset=utf-8',
error: function (response)
{
alert(JSON.stringify(response));
},
success(function (response)
{
alert(JSON.stringify(response));
}
})
而不是
$.ajax({
type: 'POST',
url:'@Url.Action("Index", "Home")',
contentType: 'application/json; charset=utf-8',
error: function (response) { alert(JSON.stringify(response)); }
}).success(function (response) {
alert(JSON.stringify(response));
});
我觉得您在实际的ajax
之外获得了成功
,同时还获得了额外的)
编辑:如果您添加调试器,我们帮助您的一个简单方法就是我想Url必须是:Url:“/Home/Index”
,@SelimYıldız@Url。Action(“,”)
等同于Url:”/Home/Index”
你在控制器上发布什么?我在您的AJAX
调用中没有看到任何data
元素。你想调用GET
请求吗?@JamesS你确定吗,可能是这样的。@SelimYıldız我假设OP使用的是MVC,那么他们肯定会使用带有扩展名cshtml
的视图。我认为这个问题的问题在于OP在实际的ajax
之外获得了成功
,并且在最后添加了一个额外的)
。我猜ajaxajax
失败了,但是没有在函数中添加调试器并告诉我们,这只是猜测。
$.ajax({
type: 'POST',
url:'@Url.Action("Index", "Home")',
contentType: 'application/json; charset=utf-8',
error: function (response) { alert(JSON.stringify(response)); }
}).success(function (response) {
alert(JSON.stringify(response));
});