ASP.NET WebForms无法使用ajax从javascript调用webmethod

时间:2017-08-09 18:52:57

标签: javascript asp.net ajax

我似乎无法使这个简单的功能正常工作。

AJAX调用返回成功,但由于public partial class Contact : Page { protected void Page_Load(object sender, EventArgs e) { Debug.WriteLine("Contact Page loaded"); } [System.Web.Services.WebMethod] public static string Test(string test) { Debug.WriteLine("Input param"+test); return test; } } 未显示,因此永远不会调用该函数。 "函数已被调用"警告弹出。 Chrome控制台中没有错误。

我正在使用ASP.NET Web Forms

Contact.aspx.cs文件:

<button type="button" class="btn btn-info" onclick="ShowTest()">Test</button>

<script type = "text/javascript">
    function ShowTest() {            

        //Tried this also (prefered)
        //var res = PageMethods.Test("testMessage");

        var testMsg = 'This is the test message';

        $.ajax({
            type: "POST",
            url: "Contact.aspx/Test",
            data: JSON.stringify({
                test: testMsg
            }),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                alert('It worked! ' + result.d);
            },
            error: function (result) {
                alert('Nope');
            }
        });

        alert("Function has been called");
    }       
</script>

在Contact.aspx文件中

(def FooBar {(s/required-key :foo) s/Str (s/required-key :bar) s/Keyword})

(s/validate FooBar {:foo "f" :bar :b})
;; {:foo "f" :bar :b}

(s/validate FooBar {:foo :f})
;; RuntimeException: Value does not match schema:
;;  {:foo (not (instance? java.lang.String :f)),
;;   :bar missing-required-key}

1 个答案:

答案 0 :(得分:2)

我找到了解决方案!

Authentication failed in call webmethod from jquery AJAX

  

块引用

我找到了答案

只需在RouteConfig文件中的下方注释

// settings.AutoRedirectMode = RedirectMode.Permanent;