admin管理员组

文章数量:1336660

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?

本文标签: javascriptUncaught ReferenceError Sys is not defined on ASPNETStack Overflow