【发布时间】:2013-06-07 20:58:11
【问题描述】:
看起来我可以将文本发布到我的 REST 服务,但我无法发布 json 或看起来像 json 的文本,我不知道为什么。在底部,我包含了两个不同的 Ajax 位,一个可以工作(只传递一个字符串),另一个不工作(传递一个 Json 字符串)。当它不起作用时我得到的错误是“方法不允许”,但我怀疑这是因为它试图 POST 到 GET 方法......至少,感觉就是这样。 GET 工作正常。另外,我可以在 fiddler 中重现这个。
合同:
[ServiceContract]
public interface IUser
{
[ OperationContract ]
[ WebGet (UriTemplate = "" , ResponseFormat = WebMessageFormat.Json)]
MyUserObject GetUser();
[ OperationContract ]
[ WebInvoke (UriTemplate = "{userToUpdate}" , Method = "POST", RequestFormat = WebMessageFormat .Json, BodyStyle = WebMessageBodyStyle .WrappedRequest)]
void UpdateUser(string userToUpdate);
}
类:
[ AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode .Allowed)]
[ ServiceBehavior(InstanceContextMode = InstanceContextMode .PerCall)]
public class User : IUser
{
[ WebGet (UriTemplate = "" , ResponseFormat = WebMessageFormat.Json)]
public MyUserObject GetUser()
{
UserState thisSessionManager = new UserState();
MyUserObject returnMe = thisSessionManager.GetCurrentUser();
return returnMe;
}
[ WebInvoke (UriTemplate = "{userToUpdate}" , Method = "POST", RequestFormat = WebMessageFormat .Json, BodyStyle = WebMessageBodyStyle .WrappedRequest)]
public void UpdateUser( string userToUpdate)
{
///here I'm just logging the text value
}
}
}
路由:
private void RegisterRoutes()
{
WebServiceHostFactory factory = new WebServiceHostFactory();
RouteTable .Routes.Add(new ServiceRoute( "services/user" , factory, typeof (BigHistory.Services.User )));
}
Ajax 不起作用:
var passthis = JSON.stringify(thisUser);
$.ajax({
type: "POST" ,
url: "/services/user" ,
data: "{ 'userToUpdate':'" + passthis + "'}" ,
contentType: "application/json; charset=utf-8" ,
dataType: "json" ,
async: true ,
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
确实有效的 Ajax(“xyz123”由类中的正确函数记录):
$.ajax({
type: "POST" ,
url: "/services/user/xyz123" ,
contentType: "application/json; charset=utf-8" ,
dataType: "text" ,
async: true ,
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
Coinfig:
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="MyName.Services.User" behaviorConfiguration="myServiceBehavior">
<endpoint name="webHttpBinding" address="" binding="webHttpBinding" contract="MyName.Services.IUser" behaviorConfiguration="webHttp"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="myServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
传递给控制台的对象:
{"Email":"","FirstName":"Joe","IHaveSeenMyProfilePage":false}
任何帮助都将不胜感激。
【问题讨论】: