wcf REST服务和JQuery Ajax Post:不允许使用方法

谁知道这有什么问题? 我无法从我的wcfrest服务获得json响应。

jQuery的

$.ajax({ type: 'POST', url: "http://localhost:8090/UserService/ValidateUser", data: {username: 'newuser', password: 'pwd'}, contentType: "application/json; charset=utf-8", success: function(msg) { alert(msg); }, error: function(xhr, ajaxOptions, thrownError) { alert('error'); } }); 

服务

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class UserService: IUserService { private readonly IUserRepository _repository; public UserService() { _repository = new UserRepository(); } public ServiceObject ValidateUser(string username, string password) { //implementation } } [ServiceContract] public interface IUserService { [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] [OperationContract] ServiceObject ValidateUser(string username, string password); }
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class UserService: IUserService { private readonly IUserRepository _repository; public UserService() { _repository = new UserRepository(); } public ServiceObject ValidateUser(string username, string password) { //implementation } } [ServiceContract] public interface IUserService { [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] [OperationContract] ServiceObject ValidateUser(string username, string password); } 

网络配置

                             
                              

我在你的代码中看到了多个问题:

405表示不允许的方法 – 这可能意味着您将数据发布到错误的资源。 你确定你的地址是正确的吗? 你如何公开这项服务? 是.svc文件还是ServiceRoute ? 如果是.svc文件,则地址为UserService.svc/UserService/ValidateUser

  • UserService.svc,因为这是您的服务的入口点(如果您使用ServiceRoute您可以重新定义它
  • UserService,因为您在端点配置中定义了此相对地址
  • ValidateUser,因为这是您的操作的默认入口点

现在您的JSON请求完全错误,您的方法签名也是如此。 服务合同中的方法签名必须要求单个JSON对象=它必须是单个数据协定,如:

 [DataContract] public class UserData { [DataMember] public string UserName { get; set; } [DataMember] public string Password { get; set; } } 

和操作签名将是:

 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)] [OperationContract] ServiceObject ValidateUser(UserData userData); 

JSON请求中没有包装元素,因此必须使用Bare 。 此外,不需要设置响应格式,因为您将在端点级别设置它(顺便说一下,如果不这样做,还必须设置请求格式)。

为请求定义数据协定后,您必须正确定义ajax请求本身:

 $.ajax({ type: 'POST', url: "UserService.svc/UserService/ValidateUser", data: '{"UserName":"newuser","Password":"pwd"}', contentType: "application/json; charset=utf-8", success: function (msg) { alert(msg); }, error: function (xhr, ajaxOptions, thrownError) { alert('error'); } }); 

JSON对象是字符串! 及其所有成员!

最后将配置修改为:

              

如果要使用standardEndpoint ,则必须在端点定义中使用kind ,并且不需要指定行为(它是标准端点的一部分)。 此外,您不使用跨域调用,因此您不需要启用它们,并且您不需要默认格式,因为它会自动解决。

我相信伊万在这里走在正确的轨道上!

你是在浏览器中通过javascript调用你的服务,对吗?

带有该JavaScript的html页面是否与wcf服务位于同一个域中?

如果他们不在同一个域中 ,那么我会说这是一个跨站点脚本问题。 我相信GET是允许跨站点,但POST不是。 http://en.wikipedia.org/wiki/JSONP将是一个解决方案,如果它支持服务器端(通过WCF)

您在一个域上进行了测试,我认为作者尝试从不同的域进行调用。 由于跨域调用,这可能是不可能的。