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

h3n*_*h3n 4 ajax wcf jquery json wcf-rest

谁知道这有什么问题?我无法从我的wcf休息服务获得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);
    }


网络配置



 <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>


Lad*_*nka 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; }
}
Run Code Online (Sandbox Code Playgroud)

和操作签名将是:

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

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

});
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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