2011-07-09 113 views
4

任何人都知道这有什么问题吗?我无法得到我的wcf休息服务的JSON响应。wcf REST服务和JQuery Ajax Post:不允许使用方法

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); 
    } 

 

web配置

 


<system.serviceModel> 

    <!--Behaviors here.--> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="defaultEndpointBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 

     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <!--End of Behaviors--> 

    <!--Services here--> 
    <services> 
     <service name="MyWcf.Services.UserService"> 
     <endpoint address="UserService" behaviorConfiguration="defaultEndpointBehavior" 
      binding="webHttpBinding" contract="MyWcf.Services.IUserService" /> 
     </service> 
    </services> 

    <!--End of Services--> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <standardEndpoint name="" 
          helpEnabled="true" 
          automaticFormatSelectionEnabled="true" 
          defaultOutgoingResponseFormat ="Json" 
          crossDomainScriptAccessEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

 

回答

9

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

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对象是字符串!和所有的成员!

对于最后修改你的配置:

<system.serviceModel> 
    <services> 
    <service name="UserService.UserService"> 
     <endpoint address="UserService" kind="webHttpEndpoint" contract="UserService.IUserService" /> 
    </service> 
    </services> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <standardEndpoints> 
    <webHttpEndpoint> 
     <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" /> 
    </webHttpEndpoint> 
    </standardEndpoints> 
</system.serviceModel> 

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

+0

我正在使用WCF没有.svc文件。 – h3n

+0

我跟着你的修改,当我尝试它时,它说:合同'IUserService'的'ValidateUser'操作指定了多个请求主体参数,在没有任何包装器元素的情况下被序列化。至多有一个身体参数可以在没有包装元素的情况下被序列化。删除额外的主体参数或将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为Wrapped。 – h3n

+0

所以你没有按照我的修改,因为我的'ValidateUser'没有多个请求主体参数。 –

1

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

2

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

您正在浏览器中调用JavaScript的服务,对不对?

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

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

+0

您提到过POST不允许在跨域WCF REST JQuery Ajax Call中使用。你能给我来源吗?因为从您的帖子中,我只学习了POST不支持跨域。由于安全原因,我想使用POST,但它不起作用。 – Firnas

+0

在http://en.wikipedia.org/wiki/Same_origin_policy中有相当好的解释,但如果您正在寻找答案,您可以在这里找到一些建议,例如, http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – Joakim

相关问题