$.ajax POST to my webservice works fine but GET requests fail with error 405 Method not allowed
For a while now, I've been using web services regardless of the purpose with the jquery $.ajax type: "POST". It has always gotten the job done so I never really looked into using type: "GET". I just tested "GET" out today because this plugin I'm using (EXT Gantt) requires GET and I got this Method Not Allowed (405) error in Firebug. I think I need to enable my webservice to accept GET requests. How do I do that?
Interface:
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
List<Task> GetAllTasks();
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
List<Dependency> GetAllDependencies();
Class:
[DataContract]
public class Task
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string StartDate { get; set; }
[DataMember]
public string EndDate { get; set; }
[DataMember]
public int PercentDone { get; set; }
[DataMember]
public int? ParentId { get; set; }
[DataMember]
public bool IsLeaf { get; set; }
[DataMember]
public string Responsible { get; set; }
[DataMember]
public int Duration { get; set; }
}
[DataContract]
public class Dependency
{
[DataMember]
public int From { get; set; }
[DataMember]
public int To { get; set; }
[DataMember]
public int Type { get; set; }
}
Methods:
public List<Task> GetAllTasks()
{
List<Task> Tasks = new List<Task>()
{
};
Task temp = new Task();
temp.Id = 0;
temp.Name = "Planning";
temp.StartDate = "2010-01-06T00:00:00";
temp.EndDate = "2010-01-21T00:00:00";
temp.PercentDone = 40;
temp.ParentId = null;
temp.IsLeaf = false;
temp.Responsible = "John Doe";
temp.Duration = 0;
Tasks.Add(temp);
return Tasks;
}
public List<Dependency> GetAllDependencies()
{
List<Dependency> Dependencies = new List<Dependency>()
{
};
Dependency temp = new Dependency();
temp.From = 11;
temp.To = 12;
temp.Type = 2;
Dependencies.Add(temp);
return Dependencies;
}
Javascript/Ajax: (again, this works perfectly when using type: "POST")
$.ajax({
type: "GET",
async: false,
url: "Services/ProjectService.svc/GetAllDependencies",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, xhr) { }
});
$.ajax({
type: "GET",
async: false,
url: "Services/ProjectService.svc/GetAllTasks",
contentType: "application/json; charset=utf-8",
dataType:开发者_如何学编程 "json",
success: function (data, textStatus, xhr) { }
});
Web.Config
<system.serviceModel>
<services>
<service behaviorConfiguration="ProjectScheduler.Services.ProjectServiceBehavior" name="ProjectScheduler.Services.ProjectService">
<endpoint address="" behaviorConfiguration="http" binding="webHttpBinding" bindingConfiguration="" contract="ProjectScheduler.Services.IProjectService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport">
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="http">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ProjectScheduler.Services.ProjectServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
try changing the interface to
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method="GET")]
List<Task> GetAllTasks();
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method="GET")]
List<Dependency> GetAllDependencies();
精彩评论