It looks like I can post text to my REST service, but I can't post json or text that looks like json, and I can't figure out why. At the bottom I've included two different Ajax bits, one that works (only passing a string) and one that doesn't (passing a string that's Json). The error I get when it doesn't work is "Method Not Allowed", but I suspect that's because it's trying to POST to the GET method...at least, that's what it feels like. The GET works fine. Also, I can repro this in fiddler.
Contract:
[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);
}
Class:
[ 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
}
}
}
Routing:
private void RegisterRoutes()
{
WebServiceHostFactory factory = new WebServiceHostFactory();
RouteTable .Routes.Add(new ServiceRoute( "services/user" , factory, typeof (BigHistory.Services.User )));
}
Ajax that doesn't work:
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 that does work ("xyz123" is logged by the correct function in the class):
$.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>
The object, as passed to the console:
{"Email":"","FirstName":"Joe","IHaveSeenMyProfilePage":false}
Any help at all would be appreciated.