Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda