Reputation: 11871
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
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