gpt4 book ai didi

jquery - wcf REST 服务和 JQuery Ajax 帖子 : Method not allowed

转载 作者:行者123 更新时间:2023-11-30 23:52:34 25 4
gpt4 key购买 nike

有人知道这是怎么回事吗?我无法从 WCF Rest 服务获取 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>

最佳答案

我在您的代码中发现多个问题:

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,并且不需要指定行为(它是标准端点的一部分)。此外,您没有使用跨域调用,因此不需要启用它们,也不需要默认格式,因为它会自动解析。

关于jquery - wcf REST 服务和 JQuery Ajax 帖子 : Method not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6633648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com