wcf REST服务和JQuery Ajax Post:不允许使用方法
有人知道这是怎么回事吗?我无法从我的wcf rest服务获取json响应 Jquerywcf REST服务和JQuery Ajax Post:不允许使用方法,jquery,ajax,wcf,json,wcf-rest,Jquery,Ajax,Wcf,Json,Wcf Rest,有人知道这是怎么回事吗?我无法从我的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)
$.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,因为这是操作的默认入口点
[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对象是string!及其所有成员
最后,请将配置修改为:
<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>
如果要使用
标准端点
,则必须在端点定义中使用种类
,而无需指定行为(它是标准端点的一部分)。另外,您没有使用跨域调用,因此不需要启用它们,也不需要默认格式,因为它是自动解析的。您在一个域上进行了测试,我想作者尝试从不同的域进行调用。由于跨域调用,这可能是不可能的。我相信Ivan在这方面是正确的
您正在浏览器中从javascript调用服务,对吗
带有该javascript的html页面是否与wcf服务位于同一域中
如果它们不在同一个域中,那么我会说这是一个跨站点脚本问题。我相信GET允许跨站点,但POST不允许。如果服务器端(由WCF)支持,这将是一个解决方案我跟踪了您的修改,尝试修改时,它显示:契约“IUserService”的操作“ValidateUser”指定要序列化的多个请求正文参数,而不使用任何包装器元素。最多可以序列化一个正文参数,而不使用包装器元素。请删除额外的正文参数或将BodyStyle属性设置为onWebGetAttribute/WebInvokeAttribute为Wrapped。因此,您没有按照我的修改进行操作,因为我的
ValidateUser
没有多个请求正文参数。我已将参数更改为UserData,并在运行该参数时,“不允许使用方法”。在Global.asax中,我放置ff,它可以工作:HttpContext.Current.Response.AddHeader(“访问控制允许来源”、“*”;HttpContext.Current.Response.AddHeader(“访问控制允许方法”、“获取、发布”);HttpContext.Current.Response.AddHeader(“访问控制允许头”、“内容类型、接受”);HttpContext.Current.Response.End();是否有一种方法可以将数据发布到服务中,而无需在全局.asax上执行任何操作?是的,有一种方法是因为我使用jquery和本地IIS上托管的页面和服务对.aspx页面进行了准确的更改测试,并且该方法工作正常,无需修改标题。您提到的发布是不允许的在跨域WCF REST JQuery Ajax调用中遇到了问题。你能告诉我问题的来源吗?因为,从你的帖子中我了解到,帖子不支持跨域。出于安全考虑,我想使用帖子,但它不起作用。在中解释得很好,但如果你正在寻找答案,你可以在这里找到一些建议,例如。