0

The Simple code below

<div class="btn btn-primary" id="btnSearch">Search</div>

<script src="<% = Page.ResolveUrl("~/Scripts/jquery-latest.js") %>"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnSearch').click(function () {
                $.ajax({
                    method: 'POST',
                    url: 'MenuBackoffice.aspx/onSearch',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: { },
                    success: function (msg) {

                    }
                });
            });
        });
    </script>

    [WebMethod]
    public static void onSearch()
    {

    }

Is not reaching my WebMethod, i don't know what is wrong, can someone add some light?

EDIT: In the debug i'm getting the following error: ailed to load resource: the server responded with a status of 500 (Internal Server Error)

10
  • is jquery loading ? Commented Dec 28, 2016 at 14:46
  • and also have you tried to put a / in front of your url? .. like : url: '/MenuBackoffice.aspx/onSearch', Commented Dec 28, 2016 at 14:49
  • That is a server-side error (the clue is in the "Internal Server Error") :) Enable full errors display on your server to see the actual bug Commented Dec 28, 2016 at 14:49
  • @federicoscamuzzi: It would have been a 404 if that were the problem. Commented Dec 28, 2016 at 14:50
  • It could certainly be reaching your WebMethod, which then produces an unhandled exception. Add debugging or logging code to make sure what is happening. Commented Dec 28, 2016 at 14:58

2 Answers 2

1

I had to clean and build the solution and after that the error changed to "An attempt was made to call the method 'onSearch' using a GET request, which is not allowed."

EDIT: Adding Nagaraj Raveendran solution - Instead use GET in ajax method, without having to use the ScriptMethod.

Sign up to request clarification or add additional context in comments.

2 Comments

Good. Instead try using GET in ajax method, without having to use the ScriptMethod.
That's even better. Edited.
0

add the below code to routeconfig.

routes.IgnoreRoute("MenuBackoffice.aspx/onSearch");

If this does not work, URL might be an issue. Try changing url: in ajax call.

'<%= ResolveUrl("MenuBackoffice.aspx/onSearch") %>'

2 Comments

Suggested an another change you could try to make it work in the answer.
Got the same error: Unknown web method onSearch. Parameter name: methodName

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.