簡體   English   中英

頁面加載時 Ajax 調用未命中控制器

[英]Ajax Call not hitting controller when page is loaded

我有一個 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>

控制器

public ActionResult Index()
{
    return Json(new { success = true, rtn = "TEST" }, JsonRequestBehavior.AllowGet);
}

這個alert(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之外取得了success ,並且還有一個額外的)

編輯:如果您添加debugger; ,我們可以幫助您的一種簡單方法debugger; 進入函數本身並告訴我們它是否在 Google DevTools 中引發錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM