user979331
user979331

Reputation: 11871

ASP.NET and jQuery Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

I am trying to make an ajax call to an ASP.NET Controller, but keep getting this error:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

Here is the ajax code:

$.ajax({
        type: "POST",
        url: "api/connection/getCommunities",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (data) {
            var communityDropdown = $("#communtiyDropdown");
            $.each(data, function () {
                communityDropdown.append($("<option />").val(data.Job_NO).text(data.Job_NO));
            });
        }
    });

and here is the controller:

public class ConnectionController : ApiController
    {
        [WebMethod]
        public List<CommunityClass> getCommunities()
        {
            ConnectionClass jobs = new ConnectionClass();
            return jobs.getCommunities();
        }
    }

what am I doing wrong? When I goto the URL directly it works. But when I try to call it in an ajax call it does not work.

Upvotes: 0

Views: 6696

Answers (1)

John Wu
John Wu

Reputation: 52250

It's possible there are web.config settings preventing POST from being used on that service. Change your AJAX code to use GET and see if that works.

$.ajax({
        type: "GET",
        url: "api/connection/getCommunities",
        dataType: 'json',
            contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (data) {
            var communityDropdown = $("#communtiyDropdown");
            $.each(data, function () {
                    communityDropdown.append($("<option />").val(data.Job_NO).text(data.Job_NO));
            });
        }
    });

Upvotes: 1

Related Questions