Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载页面时Ajax调用未命中控制器_C#_Jquery_Ajax_Model View Controller - Fatal编程技术网

C# 加载页面时Ajax调用未命中控制器

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

我有一个ajax调用,它在另一个页面上似乎工作得很好,但是当我在这个页面上调用它时,它就不工作了

CSHTML页面

<!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:“/Home/Index”
,@SelimYıldız
@Url。Action(“,”)
等同于Url:
”/Home/Index”
你在
控制器上发布什么?我在您的
AJAX
调用中没有看到任何
data
元素。你想调用
GET
请求吗?@JamesS你确定吗,可能是这样的。@SelimYıldız我假设OP使用的是MVC,那么他们肯定会使用带有扩展名
cshtml
的视图。我认为这个问题的问题在于OP在实际的
ajax
之外获得了
成功
,并且在最后添加了一个额外的
。我猜ajax
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));
            });