Javascript function does not run Pagemethod

Asked

Viewed 243 times

2

I’m calling a Pagemethod in a Javascript function, but it goes straight to the end not entering my function C#. Follow 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;
    }
  • add the code of your methods and what error you are having, so we will be able to help you :)

  • Even if I create the code with a deliberate error to try to simulate your mistake, my code will be different than yours. Post your code friend so we can view.

1 answer

1

On the page of MSDN you get an example.

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

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

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

In case of any problem, you can consume your method via ajax too.

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
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.