javascript - Uncaught ReferenceError: Sys is not defined on ASP.NET - Stack Overflow

I want use ajax on ASP.NET platform. For that I use ScriptManager. Simply I add this script with jQuery

I want use ajax on ASP.NET platform. For that I use ScriptManager. Simply I add this script with jQuery after when "document is ready".

$(document).ready(function () {

    // sync
    {{scopeName}}_init();

    // async

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
        {{scopeName}}_init();
    });

});

And then such a mysterious javascript error happened.

Uncaught ReferenceError: Sys is not defined

Q what I have wrong if javascript stop working after first request?

I want use ajax on ASP.NET platform. For that I use ScriptManager. Simply I add this script with jQuery after when "document is ready".

$(document).ready(function () {

    // sync
    {{scopeName}}_init();

    // async

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
        {{scopeName}}_init();
    });

});

And then such a mysterious javascript error happened.

Uncaught ReferenceError: Sys is not defined

Q what I have wrong if javascript stop working after first request?

Share Improve this question asked Feb 23, 2016 at 20:11 BrunoBruno 7,2215 gold badges44 silver badges49 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

Solution is not skip "Sys.WebForms.PageRequestManager" using undefined condition

if (typeof(Sys) !== 'undefined') {
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
        {{scopeName}}_init();
    });
}

For the postback we init function just if document is ready - sync section.

For ajax request in ScriptManager block we need to register init function on "add_pageLoaded". After that everytime script works fine at first request - async section.

Most important neglected step is register also "add_endRequest", and that fix the problem.

Whole example code:

$(document).ready(function () {

    // sync
    console.log("sync");
    {{scopeName}}_init();

    // async

    pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

    pageRequestManager.add_endRequest({{scopeName}}_onAsyncEndRequest);
    pageRequestManager.add_pageLoaded({{scopeName}}_onAsyncPageLoaded);


});

function {{scopeName}}_onAsyncEndRequest(sender, args) {
    console.log("async end");
    console.log(args);
}
function {{scopeName}}_onAsyncPageLoaded(sender, args) {
    console.log("async start");
    {{scopeName}}_init();
}

I recently ran across this problem myself so I figured I would type this up to help someone who might be going through the same/similar issue.

What I found out was that when VS deployed the application, and I started running the app on my production server, my browser was looking for ScriptResource.axd (for ASP.NET AJAX) and WebResource.axd, and not finding either file in the root directory.

More on these files: What is WebResource.axd?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742418051a4440127.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信