1

I have a simple page. On load, it calls a web service, and then I get the following error:

an attempt was made to call the method using a GET request, which is not allowed

My JS-code:

    function getTutors() {
        var url = '<%= ResolveUrl("~/services/tutorservice.asmx/gettutors") %>';
        $.ajax({
            type: "GET",
            data: "{'data':'" + 'test-data' + "'}",
            url: url,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (d) {
                alert('succes');
                return d;
            },
            error: function () {
                alert('fejl');
            }
        });
    }

    $(document).ready(function () {
        var tutors = getTutors();
        var locations = [];
    }

My web service:

    [ScriptService]
public class tutorservice : System.Web.Services.WebService {

    public tutorservice () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public List<Tutor> gettutors(string data)
    {
        var tutorManager = new TutorManager();
        return tutorManager.GetTutorApplicants();
    }

}

I have tried to remove the contentTypes, and even without the data variable, it still gives the same error.

My best guess is some contentType / dataType should be removed, but I have tried that as well.

Any ideas about why I get this error?

1 Answer 1

5

I can think of two options:

1) Use a POST instead of a GET in your AJAX call:

type: "POST",

or 2) If you must use a GET, configure your web service method to allow GETS:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Tutor> gettutors(string data)
{
    var tutorManager = new TutorManager();
    return tutorManager.GetTutorApplicants();
}

and allow GETS via web.config:

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>
Sign up to request clarification or add additional context in comments.

1 Comment

Solved my problem with choosing POST instead... Thanks!

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.