有一段时间了,我一直在使用web服务而不管jquery $ .ajax类型的目的:“POST”。它总是完成工作,所以我从来没有真正考虑使用类型:“GET”。我今天刚刚测试了“GET”因为我正在使用的这个插件(EXT Gantt)需要GET而我在Firebug中遇到了这个方法不允许(405)错误。我想我需要启用我的web服务来接受GET请求。我该怎么做?
接口:
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
List<Task> GetAllTasks();
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
List<Dependency> GetAllDependencies();
类别:
[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; }
}
方法:
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 :(再次,这在使用类型:“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>
答案 0 :(得分:1)
尝试将界面更改为
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method="GET")]
List<Task> GetAllTasks();
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method="GET")]
List<Dependency> GetAllDependencies();