1

I have a very simple page and a [WebMethod] inside it which returns a simple message. I would like to show this message through $.ajax on client side. however my website is using rewrites rules so my url becomes readable to user.

EX: Actual webpage: www.mysite.com/about // which has about folder and a user control inside it

there is no aspx page for this instead i am using a method which gets a webpage data which is actual html page and show the content on user control.

here is Jquery part.

$(document).ready(function () {
    $('.info a').click(function () {
        $.ajax({
            type: 'POST',
            url: '/about/showServer', //which url to put here
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                alert("result.d");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            },
        });
    });
}); 

C#

[WebMethod] // this method is in the user control
      public static string showServer()
      {
            return "Hello from server";
      }

How to call this method from client using $.ajax

appreciate your time and help.

EDITS

I have this structure for my website

mysite.com/about

/about/defualt.aspx --> which loads the user controls

user controls resides in

mysite.com/ConLib/Custom/about.ascx/showServer

So i set it to like this

url: '/ConLib/Custom/about.ascx/showServer',

BUT i see error in chrome developer tool in XHR request "404 error" because when you type mysite.com/conlib/blah blah ..reqrites does not allows this and throws 404 error..

4
  • 1
    Is using webservice? If yes, you removed the comment of this line? [System.Web.Script.Services.ScriptService] Commented May 17, 2013 at 14:00
  • no i amusing normal method Commented May 17, 2013 at 14:05
  • Where is your webmethod? In an MVC controller or Webforms aspx page? Commented May 17, 2013 at 14:13
  • webforms..in ascx ..see my edits please Commented May 17, 2013 at 14:22

3 Answers 3

1

Your ajax success method should be this:

alert(result.d);

Instead of this:

success: function (result) {
    alert("result.d");
}

and url should be:

url: "Default.ascx/showServer",   // UserControlPage/MethodName
Sign up to request clarification or add additional context in comments.

8 Comments

now its shows "Error" in alert box..when i see in chrome developer tool i see this Exception Details: System.ArgumentException: Unknown web method showServer. Parameter name: methodName.. i check the method name and its exactly same i mentioned in my jquery –
What is the url path you have set?
How do you register your user control in defualt.aspx page? What is the path set there?
thorugh regiter directive in pgae
What is the Src you have set for that <%@ Register %> directive??
|
1

you need to decorate your web method

  [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json)]
  public static string showServer()
  {
        return "Hello from server";
  }

5 Comments

now its shows "Error" in alert box..when i see in chrome developer tool i see this Exception Details: System.ArgumentException: Unknown web method showServer. Parameter name: methodName.. i check the method name and its exactly same i mentioned in my jquery
try mysite.com/about/showServer or localhost/about/showServer as the url
url: 'localhost/ConLib/Custom/about.ascx/showServer/' add the / on the end?
you added the data: "{}" ?
and does your localhost have a port number for example localhost:40899/ConLib/Custom/about.ascx/showServer
1

If your WebMethod is inside a User Control, then it needs to be moved to the ASPX page. See this post:

How to call an ASP.NET WebMethod in a UserControl (.ascx)

The url: param should be in the form of '/MyPage.aspx/showServer'

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.