看起来我可以将文本发布到我的REST服务,但是我不能发布看起来像json的json或文本,我无法找出原因。在底部,我包含了两个不同的Ajax位,一个工作(只传递一个字符串)和一个不工作(传递一个字符串的Json)。我不能使用的错误是“方法不允许”,但我怀疑是因为它试图POST到GET方法...至少,这就是它的感觉。 GET工作正常。另外,我可以用小提琴手重复这个。
合同:
[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}
任何帮助都将不胜感激。
答案 0 :(得分:0)
改变了这个:
UriTemplate = "{userToUpdate}"
对此:
UriTemplate = ""
一切都很好。