Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
how to call aspx.vb method using ajax

how to call vb.net method from ajax I send array as parameter in the ajax and it's not working this is the ajax and javascript code

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>   

        <script type="text/javascript">
                $("#Button1").click(function () {
                    var Years = document.getElementById("Options").querySelectorAll(".selected");
                   


                    $.ajax({
                        type: "POST",
                        url: "NewScenarioProfilePage.aspx/CalculateSum",
                        data: "{'yearvalue ':'" + Years  +  "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: OnSuccess,
                        failure: function (response) {
                     ' this alert didn't show 
                            alert(response.d);
                        }

                    });

                });

and this is the vb method P.s this yearvalue maybe be an array if it's more than 2 values or it will be an integer variable if it's one value

    <System.Web.Services.WebMethod()>
 
 Public Shared Function CalculateSum(ByVal yearvalue as Integer) As String
        MsgBox("calculate sum")

        Dim Result As Integer = yearvalue 
        Return Result.ToString()
    End Function

and the msgbox doesn't fire. I don't know what the problem is. this is the error in the inspect when I click on the button

jquery.min.js:4 POST https://localhost:44351/NewScenarioProfilePage.aspx/CalculateSum 401
send    @   jquery.min.js:4
ajax    @   jquery.min.js:4
(anonymous) @   NewScenarioProfilePage:477
dispatch    @   jquery.min.js:3
r.handle    @   jquery.min.js:3
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You got a 401 status code as a response. So, probably you missing authentication mechanism.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Hum, I think you need to remove that server side msgbox - it will BIG TIME mess this up, since ajax calls are async.

Try this example:

Lets drop in two text boxes, a button, and take the value from first text box, run it through the function (say times 2 for fun).

So we have this for the server code:

<System.Web.Services.WebMethod()>
Public Shared Function CalculateSum(ByVal yearvalue As Integer) As String

    Debug.Print("cal sum run!!")

    Dim Result As Integer = yearvalue * 2

    Return Result.ToString()

End Function

And for client side? lets not just have the js code kind of "sitting" there - lets call a routine, and do the post.

So lets drop in a standrd asp.net button - but NOTE how we ONLY call client side code with that button (onClientClick). and we use return false so the button does not post-back.

    <div>
        Enter year:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <br />
        <asp:Button ID="Button1" runat="server" Text="Test Ajax call"
            OnClientClick="mytest();return false"/>
        <br />
        Result ajax from server: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </div>

    <script>
        function mytest() {

            myyear = $('#TextBox1').val()

            $.ajax({
                type: "POST",
                url: "AjaxTestFun.aspx/CalculateSum",
                data: JSON.stringify({ yearvalue: myyear }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (rData) {
                    // put results send back into TextBox2
                    $('#TextBox2').val("Result from server = " + rData.d)
                },
                failure: function (rData) {
                    alert("error " + rData.d);
                }
            });
        }

    </script>

So, the output looks now like this:

enter image description here

So, I used a debug.print. Use that or console log, since when you start using ajax calls - then a server side msgbox can often hurt you.

And if the current web page works fine, then tossing in a web method also will work (it it NOT an authentication issue).

I also used JSON.stringify. It "assumes" the first value is text, and the 2nd value can be a variable like I used such as mymyear, or you could use '12335' under quotes.

So that stringify REALLY helps world poverty and suffering by you not have to try and concatenate up a bunch of variables, and a HUGE mess of single or double quotes.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda