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

141
Vistas
cannot read property of undefined (reading 'ajax')

I can't access webmethod within javascript. It gives the error in the title. Why might it be caused?

Js :

    function funcGoster() {
          $.ajax({
              type: "POST",
              url: "/WebService1.asmx/HelloWorld",
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                //  document.getElementById('text').innerHTML = 
              },
              error: function (e) {
                  alert("başarısız" + e);

              }
          });
      }
</script>

WebMethod :

public class WebService1 : System.Web.Services.WebService
{

    [WebMethod]
    public static string HelloWorld()
    {
        return "Hello World";
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Ok, what you have looks quite good, but note in the web service page, you have to un-comment the one line.

so, you should have this:

{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

Ok, now our code and markup:

    <asp:Button ID="Button1" runat="server" Text="Button" Width="153px"
        OnClientClick="ajtest();return false;"/>
    <br />
    </div>
    <script>
        function ajtest() {
            $.ajax({
                type: "POST",
                url: "/WebService1.asmx/HelloWorld",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(' back from ajax message = ' + msg.d)
                },
                error: function (xhr, status, error) {
                    var errorMessage = xhr.status + ': ' + xhr.statusText
                    alert('Error - ' + errorMessage)
                }
            });
        }
    </script>

So, we also assume you have jQuery setup for this page? You need that. You can call the web method without jQuery.

So, pure JavaScript, you could use this:

        // Web method call without jQuery

        function ajtest2() {
            // ajax call without jquery

            var xhr = new XMLHttpRequest()
            xhr.open('POST', '/WebService1.asmx/HelloWorld')
            xhr.setRequestHeader('Content-Type', 'application/json')

            xhr.send('{}');
            xhr.onload = function () {
                if (xhr.status === 200) {
                    var userInfo = JSON.parse(xhr.responseText)
                    alert(' back form ajaxes message = ' + userInfo.d)
                }
            }
        }
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