简体   繁体   中英

Uncaught ReferenceError: Sys is not defined on ASP.NET

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM