简体   繁体   中英

ASP.Net call codebehind on $.ajax success

I have a page (default.aspx) with codebehind. The a$.ajax url is getting a response from one place, and on its success I want to call the codebehind function.

(In case I made a mistake while obfuscating the code, the $.ajax works perfectly and I get the desired response).

How is this possible?

Code I'm using:

    jQuery.support.cors = true; // force cross-site scripting (as of jQuery 1.5)
    $.ajax({
                type: "POST",
                url: URL,
                data: parameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {

                    var resultCount = response.d
                    alert("*** RESULT ****" + resultFields);;
                    var string = StringReturn(); // the codebehind
                    alert(string);
                },
                error: function (e) {
                    alert("Unavailable");
                }
            });

Codebehind:

        [WebMethod]
protected static string StringReturn()
{
    return "StringReturn() success";
}

However, I'm getting error messages saying that StringReturn isn't a valid function.

Any help would be appreciated?

I've added the following lines of code to the page as advised:

    <asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true"> </asp:ScriptManager>

I've also changed the code to call a javascript function on the Success event, the function being:

    function HelloWorlds() {

        alert("HelloWorld() method");
        message = PageMethods.StringReturn();
        message.toString();
        alert(message);
    }

however that doesn't appear to work. What am I missing?

您需要在页面上有一个scripmanager,然后您可以像这样调用它PageMethods.StringReturn()

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