Calling WebMethod inside MasterPage using jQuery AJAX in ASP.Net

Last Reply 6 months ago By pandeyism

Posted 7 months ago

hi ,

i am using master page and want to execute webmethod of master page . but getting 404 not found error or 500 please check my code.

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            function calldata() {
                alert('webmethod');
                $.ajax({
                    type: "POST",
                    url: '<%=ResolveUrl("~/MainMdi.master/HelloWorld")%>' ,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.d);
                       
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
                return false;
          }
          calldata();
        });
    </script>

 

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
    public static string HelloWorld()
    {
        return "Hello World";
    }

 

You are viewing reply posted by: pandeyism 6 months ago.
Posted 6 months ago

Hi Shahzad786,

You can't call WebMethod from master page. You need to write WebMethod in aspx page.

If you want multiple content pages be able to access the WebMethod, write your code in Web Service (ASMX) instead.