Javascript function does not execute PageMethod

2

I'm calling a PageMethod in a Javascript function, however it goes straight to the end by not getting into my C # function. Here is the code below:

<script type="text/javascript">
    function enviar() {
        if (document.getElementById('<% =CPF.ClientID %>').value == "") {
            alert("Informe o documento do devedor!");
            document.getElementById('<% =CPF.ClientID %>').focus();
            return false;
        }

        PageMethods.VerificaCaixa(cartID, onSucess);

        function onSucess(result) {
            if (result != "") {
                str = result.split("|||");
                if (trim(str[0]) != "A") {
                    alert("Caixa fechado, favor abra o caixa e repita a operação!");
                    location.href = "/Caixa/Abrir/frmAbrCaixa.aspx";
                }
            }
        }
    }
</script>


<form name="frm" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
    </asp:ScriptManager>
    <table width="95%" border="0" align="center">
        <tr>
            <td class="tblNormal" align="right">CPF/CNPJ:</td>
            <td class="tblNormal" valign="middle" align="left">
                <asp:TextBox ID="CPF" runat="server" onblur='return valida();' MaxLength="14" Width="130px">
            </td>
        </tr>
        <tr>
            <td class="tblNormal" colspan="2" align="center">
                <asp:Button ID="btnEnviar" runat="server" Text="Enviar" CssClass="btn btn-default" OnClick="btnEnviar_Click" OnClientClick="enviar();" />
            </td>
        </tr>
    </table>
</form>


[WebMethod]
    [ScriptMethod]
    public static string VerificaCaixa(string cart)
    {
        CPesquisa pesquisa = new CPesquisa();

        string parametros = "";
        parametros = pesquisa.VerificaCaixa(cart);

        return parametros;
    }
    
asked by anonymous 13.11.2015 / 00:29

1 answer

1

On the MSDN page you get an example.

<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
    <Scripts >
        <asp:ScriptReference Path="myPageMethods.js" />
    </Scripts>
</asp:ScriptManager>

Your method must contain [WebMethod] to be exposed. In the file myPageMethods.js , you would do something like this.

function example(){
    myPageMethods.method();
}

In case of a problem, you can consume your method via ajax as well.

function method(){
    $.ajax({
        type: "POST",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        url: "myPage.aspx/metodo",
        data:JSON.stringify({}), // parametros de entrada do metodo
        success: function (dt) { alert(dt);}, //sucesso
        error: function () { alert('error'); } // error
    });
}
    
23.12.2015 / 14:27