Calling WebMethod inside MasterPage using jQuery AJAX in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 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";
    }

 

Posted 4 months ago
Hi @Shahzad786,
Please try the following

[Solved] PageMethod not calling WebMethod when used in Master Page in ASP.Net

It might help you.

Cheers Andrea.

Posted 4 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.