任何人都知道這有什麼問題嗎?我無法得到我的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'沒有多個請求主體參數。 –