任何人都知道这有什么问题吗?我无法得到我的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>
我正在使用WCF没有.svc文件。 – h3n
我跟着你的修改,当我尝试它时,它说:合同'IUserService'的'ValidateUser'操作指定了多个请求主体参数,在没有任何包装器元素的情况下被序列化。至多有一个身体参数可以在没有包装元素的情况下被序列化。删除额外的主体参数或将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为Wrapped。 – h3n
所以你没有按照我的修改,因为我的'ValidateUser'没有多个请求主体参数。 –