0

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";
    }
}
1
  • sounds like it's missing jQuery... include the exact error message in your post. Sometimes this happens when your script fires before jQuery is loaded... Commented Oct 27, 2021 at 21:48

1 Answer 1

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)
                }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.