需要调用jQuery ajax页面方法

时间:2012-10-13 05:10:00

标签: jquery asp.net ajax

我是jQuery的新手。我需要在一段时间后调用该方法。

    $(document).ready(function pageLoad() {
        setTimeout('SetTime2()', 10000);
    });

    (function SetTime2() {
            $.ajax({
                type: "POST",
                url: "MyPage.aspx/myStaticMethod",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    //$("#Result").text(msg.d);
                }
            });
    });

它说,Uncaught ReferenceError:SetTime2未定义。 什么是正确的语法?感谢。

1 个答案:

答案 0 :(得分:2)

更改为:

$(document).ready(function() {
    setTimeout(SetTime2, 10000);
});

function SetTime2() {
        $.ajax({
            type: "POST",
            url: "MyPage.aspx/myStaticMethod",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Replace the div's content with the page method's return.
                //$("#Result").text(msg.d);
            }
        });
}
  1. 您需要使用SetTime2()声明来定义普通函数。周围没有任何事情。

  2. 此外,您不希望将字符串传递给setTimeout(),您希望传递实际的函数引用,而不使用引号或引号。

  3. 注意:您也可以使用匿名函数执行此操作:

    $(document).ready(function () {
        setTimeout(function() {
            $.ajax({
                type: "POST",
                url: "MyPage.aspx/myStaticMethod",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    //$("#Result").text(msg.d);
                }
            });
        }, 10000);
    });
    
相关问题